Thanks.

I post the result of my tests, it could be useful later for somebody else. 
It's also more clear when you write things.

It's easy to get the context (this) and use it in JS code called from C++. 
Globals data (global scope) are also available.

I worked with "eval" and "compile". The second solution is cleaner, but I 
want to see if they act the same way.

Next I need to pass arguments (and not use "this" to pass everything), but 
main things are in place. Code is a thing, understanding is another :)

What caused me so many troubles is the lack ok "require", but now I know 
why :
- "require" is available in "this" context in REPL, but it's specific to 
REPL, then I can't expect use "this.require" everywhere. The use of REPL to 
diagnose was a trap !!!
- "require" is available in local scope in startup files, in local scope of 
modules, ... but it's not in global scope. As I don't get local scope, 
"require" is not available in my JS code called from C++ by default. I need 
to "pass it".


*C++ Addon*

  Handle<Value> UseRequire_A(const Arguments &args) {
    HandleScope scope;

    char *script_code = "require('os').hostname() + var_A + global_stuff;";

    Local<Function> eval = Local<Function>::Cast( 
Context::GetCurrent()->Global()->Get(String::New("eval")) );
    Local<Value> arguments[1] = { String::New(script_code) };
    Local<Value> result = eval->Call(Context::GetCurrent()->Global(), 1, 
arguments);

    return scope.Close(result);
  }

  Handle<Value> UseRequire_B(const Arguments &args) {
    HandleScope scope;

    char *script_code = "require('os').platform() + var_B + global_stuff;";

    Local<v8::Script> script = 
v8::Script::Compile(String::New(script_code), String::New("internal.js"));
    Local<Value> result = script->Run();

    return scope.Close(result);
  }


*Javascript *

  // "require" is JS code called from C++ , we need to associated 
  // it explicitely with the context (this)

  // Fail !
  //this.require = require;
  //this.var_A = " - Content for A";
  //this.var_B = " - Content for B";
  //var var_A = " - Content for A";
  //var var_B = " - Content for B";

  // OK
  (function() {
    this.require = require;
    this.var_A = " - Content for A";
    this.var_B = " - Content for B";
  })();

  // No - it will be local
  var global_stuff = " - Global DATA"

  // Yes - it will be global
  global_stuff = " - Global DATA"

  console.log(hello.UseRequire_A());
  console.log('After A');

  console.log(hello.UseRequire_B());
  console.log('After B');

-- 
-- 
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/d/optout.

Reply via email to