Last active
January 7, 2016 16:51
-
-
Save xaxxon/15f602eb6f6a135a97e5 to your computer and use it in GitHub Desktop.
This file contains 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
FOO | |
<unknown>:0: Uncaught ReferenceError: bar is not defined | |
# | |
# Fatal error in v8::ToLocalChecked | |
# Empty MaybeLocal. | |
# | |
Illegal instruction: 4 |
This file contains 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
#include <iostream> | |
#include <stdio.h> | |
#include <assert.h> | |
#include "include/libplatform/libplatform.h" | |
#include "include/v8.h" | |
// bog standard allocator code from V8 Docs | |
class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator { | |
public: | |
virtual void* Allocate(size_t length) { | |
void* data = AllocateUninitialized(length); | |
return data == NULL ? data : memset(data, 0, length); | |
} | |
virtual void* AllocateUninitialized(size_t length) { return malloc(length); } | |
virtual void Free(void* data, size_t) { free(data); } | |
}; | |
int main(int argc, char* argv[]) { | |
// Initialize V8. | |
v8::V8::InitializeICU(); | |
v8::V8::InitializeExternalStartupData(argv[0]); | |
v8::Platform* platform = v8::platform::CreateDefaultPlatform(); | |
v8::V8::InitializePlatform(platform); | |
v8::V8::Initialize(); | |
// Create a new Isolate and make it the current one. | |
ArrayBufferAllocator allocator; | |
v8::Isolate::CreateParams create_params; | |
create_params.array_buffer_allocator = &allocator; | |
v8::Isolate* isolate = v8::Isolate::New(create_params); | |
{ | |
v8::Isolate::Scope isolate_scope(isolate); | |
// Create a stack-allocated handle scope. | |
v8::HandleScope handle_scope(isolate); | |
v8::Local<v8::ObjectTemplate> global_templ = v8::ObjectTemplate::New(isolate); | |
global_templ->Set(isolate, "foo", v8::String::NewFromUtf8(isolate, "FOO")); | |
v8::Local<v8::Context> x_context = v8::Context::New(isolate, NULL, global_templ); | |
global_templ->Set(isolate, "bar", v8::String::NewFromUtf8(isolate,"BAR")); | |
v8::Context::Scope context_scope_x(x_context); // <=== SAME RESULT IF THIS LINE IS MOVED UP ONE | |
v8::Local<v8::String> source = | |
v8::String::NewFromUtf8(isolate, "foo", | |
v8::NewStringType::kNormal).ToLocalChecked(); | |
v8::Local<v8::Script> script = v8::Script::Compile(x_context, source).ToLocalChecked(); | |
v8::Local<v8::Value> result = script->Run(x_context).ToLocalChecked(); | |
v8::String::Utf8Value utf8(result); | |
printf("%s\n", *utf8); | |
v8::Local<v8::String> source2 = | |
v8::String::NewFromUtf8(isolate, "bar", | |
v8::NewStringType::kNormal).ToLocalChecked(); | |
v8::Local<v8::Script> script2 = v8::Script::Compile(x_context, source2).ToLocalChecked(); | |
v8::Local<v8::Value> result2 = script2->Run(x_context).ToLocalChecked(); | |
v8::String::Utf8Value utf82(result2); | |
printf("%s\n", *utf82); | |
} | |
// Dispose the isolate and tear down V8. | |
isolate->Dispose(); | |
v8::V8::Dispose(); | |
v8::V8::ShutdownPlatform(); | |
delete platform; | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment