summaryrefslogtreecommitdiff
path: root/Lib/javascript/jsc/std_string.i
blob: fb1bd62b51882d292857e20bb637f62abe28f36c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* -----------------------------------------------------------------------------
 * std_string.i
 *
 * Typemaps for const std::string&.
 * To use non-const std::string references use the following %apply:
 *      %apply const std::string & {std::string &};
 *
 * ----------------------------------------------------------------------------- */

%{
#include <string>

std::string SWIGJSC_valueToString(JSContextRef context, JSValueRef value) {
  JSStringRef jsstring = JSValueToStringCopy(context, value, /* JSValueRef *exception */ 0);
  unsigned int length = JSStringGetLength(jsstring);
  char *cstr = new char[length + 1];
  JSStringGetUTF8CString(jsstring, cstr, length + 1);

  // create a copy
  std::string result(cstr);

  JSStringRelease(jsstring);
  delete[] cstr;

  return result;
}

JSValueRef SWIGJSC_stringToValue(JSContextRef context, const std::string& s)
{
  JSValueRef result;
  JSStringRef	jsstring = JSStringCreateWithUTF8CString(s.c_str());
  result = JSValueMakeString(context, jsstring);
  JSStringRelease(jsstring);
  return result;
}
%}

namespace std {
  %naturalvar string;

  class string;


  %typemap(in) string
  %{ 
     $1 = SWIGJSC_valueToString(context, $input);
  %}

  %typemap(in) const string & 
  %{ 
     $1 = new std::string(SWIGJSC_valueToString(context, $input));
  %}

  %typemap(freearg) const string & 
  %{ 
     delete $1;
  %}

  %typemap(out) string
  %{
     $result = SWIGJSC_stringToValue(context, $1);
  %}

  %typemap(out) const string &
  %{
     $result = SWIGJSC_stringToValue(context, *$1);
  %}

}