###run.cpp:
#define BUILDING_NODE_EXTENSION
#include <node.h>
using namespace v8;
Handle<Value> run(const Arguments& args) {
HandleScope scope;
if (args.Length() < 1) {
ThrowException(Exception::TypeError(String::New("Args Error")));
return scope.Close(Undefined());
}
if (!args[0]->IsString()) {
ThrowException(Exception::TypeError(String::New("Args Error")));
return scope.Close(Undefined());
}
Persistent<Context> context = Context::New();
Context::Scope context_scope(context);
Local<Script> scritp = Script::Compile(args[0]->ToString());
Local<Value> result = scritp->Run();
return scope.Close(result);
}
void Init(Handle<Object> exports) {
exports->Set(String::NewSymbol("run"),
FunctionTemplate::New(run)->GetFunction());
}
NODE_MODULE(run, Init)
###test.js:
var addons = require('./run.node');
addons.run("console.log('hello world')");
###运行 使用命令:node test.js 报错:
ReferenceError: console is not defined
at <anonymous>:1:1
at Object.<anonymous> (C:\Users\fx\Documents\Visual Studio 2010\Projects\helloworld\Debug\test.js:2:8)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3
也不知道是啥问题,跪求高手现身
2 回复