Created
September 15, 2016 00:06
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Script::Initialize(char* src) { | |
v8::Isolate* isolate = v8::Isolate::GetCurrent(); | |
// Create a stack-allocated handle scope. | |
v8::HandleScope handle_scope(isolate); | |
// Create an instance of the global context so we can plug variables in | |
v8::Local<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate); | |
v8::Local<v8::Context> localContext = v8::Context::New(isolate, NULL, global); | |
m_context.Reset(isolate, localContext); | |
localContext->Enter(); | |
// Catch any errors the script might throw | |
v8::TryCatch try_catch(isolate); | |
// Copy source | |
v8::Local<v8::String> scriptSrc = v8::String::NewFromUtf8(isolate, src, v8::NewStringType::kNormal).ToLocalChecked(); | |
// Move stuff into global space | |
ScriptVector3::Initialize(isolate, localContext); | |
ScriptQuaternion::Initialize(isolate, localContext); | |
ScriptTransform::Initialize(isolate, localContext); | |
ScriptTime::Initialize(isolate, localContext); | |
v8::Local<v8::Script> script; | |
if (v8::Script::Compile(localContext, scriptSrc).ToLocal(&script) == false) { | |
v8::String::Utf8Value error(try_catch.Exception()); | |
ErrorSystem::HandleError(new FileError(ERROR_WARN, (char*)"Unable to compile script file", (char*)*error)); | |
return; | |
} | |
v8::Local<v8::Value> result; | |
if (script->Run(localContext).ToLocal(&result) == false) { | |
v8::String::Utf8Value error(try_catch.Exception()); | |
ErrorSystem::HandleError(new FileError(ERROR_WARN, (char*)"Unable to execute script file", (char*)*error)); | |
return; | |
} | |
// Pull out the functions we care about | |
v8::Local<v8::Object> globalSpace = isolate->GetCurrentContext()->Global(); | |
// Get the update function | |
v8::Local<v8::Value> value = globalSpace->Get(v8::String::NewFromUtf8(isolate, "Update")); | |
m_updateFunction.Reset(isolate, value.As<v8::Function>()); | |
// Make sure the update function is a function | |
v8::String::Utf8Value type(value->TypeOf(isolate)); | |
assert(value->IsFunction()); | |
// Save script | |
m_script.Reset(isolate, script); | |
localContext->Exit(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment