Add example for string marshalling to v8 specification.

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/oliverb-javascript-v8@13761 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Oliver Buchtala 2012-09-08 00:52:16 +00:00
commit 8168750c1b

View file

@ -681,6 +681,66 @@ int Class_Initialize(v8::Handle<v8::Context> context) {
} }
~~~~~ ~~~~~
## String arguments
At a first stage all strings are treated as Utf8.
For proper handling strings as return values I have to study
other modules.
~~~~~
int my_strlen(const char* s) {
return strlen(s);
}
// creates a new string
const char* foo(int a) {
char* result = new char[a+1];
result[a] = 0;
memset(result, 'a', a);
return result;
}
v8::Handle<v8::Value> wrap_my_strlen(const v8::Arguments& args)
{
v8::HandleScope scope;
v8::Handle<v8::Value> jsresult;
const char* arg1;
int result;
v8::String::Utf8Value utf8(args[0]);
result = my_strlen(*utf8);
jsresult = v8::Number::New(result);
return scope.Close(jsresult);
}
v8::Handle<v8::Value> wrap_foo(const v8::Arguments& args)
{
v8::HandleScope scope;
v8::Handle<v8::Value> jsresult;
int arg1;
const char* result;
arg1 = args[0]->Int32Value();
result = foo(arg1);
jsresult = v8::String::New(result);
return scope.Close(jsresult);
}
int Strings_Initialize(v8::Handle<v8::Context> context) {
v8::Local<v8::Object> global = context->Global();
SWIGV8_AddGlobalFunction(global, "strlen", wrap_my_strlen);
SWIGV8_AddGlobalFunction(global, "foo", wrap_foo);
return 0;
}
~~~~~
------------------------- -------------------------
Control flow analysis Control flow analysis