I searched through the group, but couldn't find any responses on this subject.
I'd like to run a script, and terminate it from C++ code, by the decision of a callback function.
I tried using TerminateExectuion(isolate), but the program crashes.
Here is a small sample. I assign values to the "specific" object using JavaScript.
Whenever the "alfa" field is set to "666", I'd like the script to terminate.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
1 #include <v8.h>
2 #include <stdio.h>
3 using namespace v8;
4
5 void SetAlfa(Local<String> property, Local<Value> value, const PropertyCallbackInfo<void>& info)
6 {
7 String::Utf8Value property_str(property);
8 String::Utf8Value value_str(value);
9 printf("SetAlfa: about to set %s=%s\n", *property_str, *value_str);
10 Handle<Value> show_stopper = String::NewFromUtf8(info.GetIsolate(),"666");
11 if (value->Equals(show_stopper))
12 {
13 printf("SetAlfa: STOPPING!\n");
14 V8::TerminateExecution(info.GetIsolate());
15 }
16 }
17
18 void RunIt(Isolate *isolate, const char *script_text)
19 {
20 printf("RunIt: about to run \"%s\"\n", script_text);
21 Handle<String> source = String::NewFromUtf8(isolate,script_text);
22 Handle<Script> script = Script::Compile(source);
23 Handle<Value> result = script->Run();
24 String::Utf8Value ascii(result);
25 printf("RunIt: returned \"%s\"\n", *ascii);
26 }
27
28
29 int main(int argc, char* argv[])
30 {
31 Isolate* isolate = Isolate::GetCurrent();
32 HandleScope handle_scope(isolate);
33 Handle<Context> context = Context::New(isolate);
34 Context::Scope context_scope(context);
35 Handle<String> alfa_str = String::NewFromUtf8(isolate,"alfa");
36 Handle<String> specific_str = String::NewFromUtf8(isolate,"specific");
37
38 Handle<Object> specific_obj = Object::New(isolate);
39 specific_obj->SetAccessor(alfa_str, NULL, SetAlfa);
40 context->Global()->Set(specific_str, specific_obj);
41
42 RunIt(isolate,"specific.alfa=123");
43 RunIt(isolate,"specific.alfa=666; specific.alfa=999");
44
45 return 0;
46 }
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
The output I get is:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
RunIt: about to run "specific.alfa=123"
SetAlfa: about to set alfa=123
RunIt: returned "123"
RunIt: about to run "specific.alfa=666; specific.alfa=999"
SetAlfa: about to set alfa=666
SetAlfa: STOPPING!
SetAlfa: about to set alfa=(null)
#
# Fatal error in ../src/api.cc, line 2899
# CHECK(!(isolate)->external_caught_exception()) failed
#
==== C stack trace ===============================
1: V8_Fatal
2: v8::Value::Equals(v8::Handle<v8::Value>) const
3: SetAlfa(v8::Local<v8::String>, v8::Local<v8::Value>, v8::PropertyCallbackInfo<void> const&)
4: v8::internal::PropertyCallbackArguments::Call(void (*)(v8::Local<v8::String>, v8::Local<v8::Value>, v8::PropertyCallbackInfo<void> const&), v8::Local<v8::String>, v8::Local<v8::Value>)
5: v8::internal::JSObject::SetPropertyWithCallback(v8::internal::Handle<v8::internal::JSObject>, v8::internal::Handle<v8::internal::Object>, v8::internal::Handle<v8::internal::Name>, v8::internal::Handle<v8::internal::Object>, v8::internal::Handle<v8::internal::JSObject>, v8::internal::StrictModeFlag)
6: v8::internal::JSObject::SetPropertyForResult(v8::internal::Handle<v8::internal::JSObject>, v8::internal::LookupResult*, v8::internal::Handle<v8::internal::Name>, v8::internal::Handle<v8::internal::Object>, PropertyAttributes, v8::internal::StrictModeFlag, v8::internal::JSReceiver::StoreFromKeyed)
7: v8::internal::JSReceiver::SetProperty(v8::internal::Handle<v8::internal::JSReceiver>, v8::internal::LookupResult*, v8::internal::Handle<v8::internal::Name>, v8::internal::Handle<v8::internal::Object>, PropertyAttributes, v8::internal::StrictModeFlag, v8::internal::JSReceiver::StoreFromKeyed)
8: v8::internal::JSReceiver::SetProperty(v8::internal::Handle<v8::internal::JSReceiver>, v8::internal::Handle<v8::internal::Name>, v8::internal::Handle<v8::internal::Object>, PropertyAttributes, v8::internal::StrictModeFlag, v8::internal::JSReceiver::StoreFromKeyed)
9: v8::internal::StoreIC::Store(v8::internal::Handle<v8::internal::Object>, v8::internal::Handle<v8::internal::String>, v8::internal::Handle<v8::internal::Object>, v8::internal::JSReceiver::StoreFromKeyed)
10: ??
11: v8::internal::StoreIC_Miss(int, v8::internal::Object**, v8::internal::Isolate*)
12: ??
-----------------------------------------------------------------------------------------------------------------------------------------------------------------