Advertisement
Guest User

calling a javascript callback function from C++

a guest
Aug 18th, 2013
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. using namespace v8;
  2.  
  3.     int
  4.     main (int argc, char *argv[])
  5.     {
  6.       //handle scope created      
  7.       HandleScope handle_scope;
  8.       //creating context
  9.       Persistent < Context > context = Context::New ();
  10.       Context::Scope context_scope (context);
  11.       // need source scrip and result
  12.       Handle < String > source;
  13.       Handle < Script > script;
  14.       Handle < Value > result;
  15.  
  16.       // Create a string containing the JavaScript source code.
  17.       source = String:: New(" var obj={name:8}; obj.abc=function(){return 9;};   function xyz() { var match = 0;if(arguments[0] == arguments[1]) { match = 1; }     return match; }   ");
  18.  
  19.       // Compile the source code.
  20.       script = Script::Compile (source);
  21.  
  22.  
  23.       // Run the script to get the result.
  24.       result = script->Run ();
  25.       // Dispose the persistent context.
  26.       context.Dispose ();
  27.  
  28.  
  29.       // create the global object
  30.       Handle < v8::Object > global = context->Global ();
  31.       Handle < v8::Value > value = global->Get (String::New ("xyz"));
  32.       Handle < v8::Function > func = v8::Handle < v8::Function >::Cast (value);
  33.       Handle < Value > js_result;
  34.  
  35.  
  36.       Handle < Value > ret = func->Call (global, 0, 0);
  37.  
  38.  
  39.     if (ret.IsEmpty ())
  40.     {
  41.         if(ret->IsNumber())
  42.         {
  43.             std::cout<<" \nis Number\n";
  44.         }
  45.         else
  46.         {
  47.             std::cout<<" \nNot a Number\n";
  48.         }
  49.     }
  50.     else
  51.     std::cout<< " is null\n";
  52.     return 0;
  53.  
  54.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement