On Wed, Sep 25, 2013 at 10:27 AM, Ashish Negi
<[email protected]> wrote:
> Yaa that is working and i am able to execute simple js code => This
> definately implies that i am getting into v8 or node main thread. But i am
> not being able to execute node specific code like
>
> var fs = require('fs');
>
> I guess since I am creating a new persistent context for myself, it would be
> independent of node's context.
> like:
>
>         v8::HandleScope handleScope;
> v8::Persistent<v8::Context> context = CreateShellContext();
> context->Enter();
> // unique_ptr makes sure that data would be deleted even if there is an
> exception.
> std::unique_ptr<char[]> codeData((char*)handle->data);
> RunCode(context, (char*)handle->data);
> context->Exit();
> context.Dispose();
>
> How can i get the context of node or is there some method to execute the
> code in node's context.

You can get the current context with v8::Context::GetCurrent().  Enter
it, then call node::MakeCallback().  That function requires a `this`
object and a function but you can always create an anonymous function
with v8::Script::New(), then call it with v8::Context::Global() as the
`this` object.  Brief example:

  Local<Context> context = Context::GetCurrent();
  Context::Scope context_scope(context);
  const char source[] = "(function(x) { return x * x; })";
  Local<String> script_source = String::New(source, sizeof(source) - 1);
  Local<Script> script = Script::New(script_source);
  Local<Value> value = script->Run();
  assert(value.IsEmpty() == false);
  assert(value->IsFunction() == true);
  Local<Function> fn = value.As<Function>();
  Local<Value> arg = Integer::New(42);
  Local<Value> rc = MakeCallback(context->Global(), fn, 1, &arg);

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to