summaryrefslogtreecommitdiff
path: root/Lib/javascript/v8/std_string.i
blob: 5ad1ead273a7bd71b657ef095268a299d9a8c6a7 (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
70
71
72
73
74
75
76
77
78
79
/* -----------------------------------------------------------------------------
 * std_string.i
 *
 * Typemaps for std::string and const std::string&.
 *
 * To use non-const std::string references use the following %apply:
 *      %apply const std::string & {std::string &};
 *
 * ----------------------------------------------------------------------------- */

%{
#include <string>
%}

%fragment("SWIGV8_valueToString", "header", fragment="SWIG_AsCharPtrAndSize") {
std::string* SWIGV8_valueToStringPtr(v8::Handle<v8::Value> val) {

  if (!val->IsString()) return 0;

  int alloc;
  size_t size;
  char* chars;
  int res = SWIG_AsCharPtrAndSize(val, &chars, &size, &alloc);

  if(res != SWIG_OK) {
    v8::ThrowException(v8::Exception::TypeError(v8::String::New("Could not convert to string.")));
    return 0;
  }

  // copies the data (again)
  std::string *str = new std::string(chars);

  if (alloc) delete[] chars;

  return str;
}
}

%fragment("SWIGV8_stringToValue", "header", fragment="SWIG_FromCharPtrAndSize") {
v8::Handle<v8::Value> SWIGV8_stringToValue(const std::string &str) {
  return SWIG_FromCharPtrAndSize(str.c_str(), str.length());
}
}

namespace std {
  %naturalvar string;

  class string;

  %typemap(in, fragment="SWIGV8_valueToString") string (std::string* tmp)
  %{
     tmp = SWIGV8_valueToStringPtr($input);
     $1 = *tmp;
     if (tmp == 0) { v8::ThrowException(v8::Exception::TypeError(v8::String::New("Null pointer."))); goto fail; }
     if (tmp) delete tmp;
  %}

  %typemap(in, fragment="SWIGV8_valueToString") const string &
  %{
     $1 = SWIGV8_valueToStringPtr($input);
     if ($1 == 0) { v8::ThrowException(v8::Exception::TypeError(v8::String::New("Null pointer."))); goto fail; }
  %}

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

  %typemap(out, fragment="SWIGV8_stringToValue") string
  %{
     $result = SWIGV8_stringToValue($1);
  %}

  %typemap(out, fragment="SWIGV8_stringToValue") const string &
  %{
     $result = SWIGV8_stringToValue(*$1);
  %}

}