JavascriptEngineV8::JavascriptEngineV8(const ScriptEngineOptions& options) : ScriptEngine(options) { _isolate = v8::Isolate::New(); v8::Locker locker(_isolate); v8::Isolate::Scope isolate_scope(_isolate); v8::HandleScope handle_scope(_isolate); _globalContext.Reset(_isolate, createGlobalContext()); if (options.script().isSet() && !options.script()->getCode().empty()) { // Create a nested handle scope v8::HandleScope local_handle_scope(_isolate); // Enter the global context v8::Local<v8::Context> globalContext = *reinterpret_cast<v8::Local<v8::Context>*>(&_globalContext); v8::Context::Scope context_scope(globalContext); // Compile and run the script ScriptResult result = executeScript(v8::String::New(options.script()->getCode().c_str(), options.script()->getCode().length())); if (!result.success()) OE_WARN << LC << "Error reading javascript: " << result.message() << std::endl; } }
void DuktapeEngine::Context::initialize(const ScriptEngineOptions& options) { if ( _ctx == 0L ) { // new heap + context. _ctx = duk_create_heap_default(); // if there is a static script, evaluate it first. This will register // any functions or objects with the EcmaScript global object. if ( options.script().isSet() ) { bool ok = (duk_peval_string(_ctx, options.script()->getCode().c_str()) == 0); // [ "result" ] if ( !ok ) { const char* err = duk_safe_to_string(_ctx, -1); OE_WARN << LC << err << std::endl; } duk_pop(_ctx); // [] } // Create the global feature object. { duk_push_global_object(_ctx); // [ global ] feature object's home duk_push_object(_ctx); // [ global feature ] empty object for starters duk_put_prop_string(_ctx, -2, "feature"); // [ global ] name it and add it to the global duk_pop(_ctx); // [] pop the global // support for the idiom: feature.attributes['attr'] duk_eval_string_noresult(_ctx, "Object.defineProperty(feature, 'attributes', {get:function() {return feature;}});"); } } }
ScriptEngine* ScriptEngineFactory::create( const std::string& language, const std::string& engineName ) { ScriptEngineOptions opts; opts.setDriver(language + (engineName.empty() ? "" : (std::string("_") + engineName))); return create(opts); }
ScriptEngine* ScriptEngineFactory::create( const Script& script, const std::string& engineName ) { ScriptEngineOptions opts; opts.setDriver(script.getLanguage() + (engineName.empty() ? "" : (std::string("_") + engineName))); opts.script() = script; return create(opts); }