Advertisement
tarruda

haskell v8 ffi

Nov 19th, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. #include <cstring>
  2. #include <cstdlib>
  3. #include <v8.h>
  4.  
  5. using namespace v8;
  6.  
  7.  
  8. extern "C" {
  9. char * hello() {
  10.   // Get the default Isolate created at startup.
  11.   Isolate* isolate = Isolate::GetCurrent();
  12.  
  13.   // Create a stack-allocated handle scope.
  14.   HandleScope handle_scope(isolate);
  15.  
  16.   // Create a new context.
  17.   Handle<Context> context = Context::New(isolate);
  18.  
  19.   // Here's how you could create a Persistent handle to the context, if needed.
  20.   Persistent<Context> persistent_context(isolate, context);
  21.  
  22.   // Enter the created context for compiling and
  23.   // running the hello world script.
  24.   Context::Scope context_scope(context);
  25.  
  26.   // Create a string containing the JavaScript source code.
  27.   Handle<String> source = String::New("'Hello' + ', World!'");
  28.  
  29.   // Compile the source code.
  30.   Handle<Script> script = Script::Compile(source);
  31.  
  32.   // Run the script to get the result.
  33.   Handle<Value> result = script->Run();
  34.  
  35.   // The persistent handle needs to be eventually disposed.
  36.   persistent_context.Dispose();
  37.  
  38.   // Convert the result to an ASCII string and print it.
  39.   String::AsciiValue ascii(result);
  40.  
  41.   char *rv = (char *)malloc(strlen(*ascii));
  42.   strcpy(rv, *ascii);
  43.  
  44.   return rv;
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement