size_t MyNearHeapLimitCallback(void* data, size_t current_heap_limit,
size_t initial_heap_limit) {
v8::Isolate *isolate = (v8::Isolate *)data;
isolate->TerminateExecution();
return initial_heap_limit + 5 * 1024 * 1024;
}
int main(int argc, char* argv[]) {
v8::V8::InitializeICUDefaultLocation(argv[0]);
v8::V8::InitializeExternalStartupData(argv[0]);
std::unique_ptr<v8::Platform> platform = v8::platform::NewDefaultPlatform();
v8::V8::InitializePlatform(platform.get());
v8::V8::Initialize();
while(true) {
v8::Isolate::CreateParams create_params;
create_params.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator();
v8::ResourceConstraints constraints;
constraints.set_max_old_space_size(10);
create_params.constraints = constraints;
v8::Isolate* isolate = v8::Isolate::New(create_params);
isolate->AddNearHeapLimitCallback(MyNearHeapLimitCallback, isolate);
{
v8::Isolate::Scope isolate_scope(isolate);
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Context> context = v8::Context::New(isolate);
v8::Context::Scope context_scope(context);
v8::Local<v8::String> source =
v8::String::NewFromUtf8(isolate, "a = []; for (;;) { a.push('hello'); }",
v8::NewStringType::kNormal)
.ToLocalChecked();
v8::Local<v8::Script> script = v8::Script::Compile(context, source).ToLocalChecked();
v8::TryCatch try_catch(isolate);
v8::MaybeLocal<v8::Value> result;
result = script->Run(context);
if (try_catch.HasCaught() && try_catch.HasTerminated()) {
isolate->CancelTerminateExecution();
}
}
delete create_params.array_buffer_allocator;
isolate->Dispose();
}
v8::V8::Dispose();
v8::V8::ShutdownPlatform();
return 0;
}