MCPcopy Create free account
hub / github.com/LibreSprite/LibreSprite / raiseEvent

Method raiseEvent

src/script/v8/engine.cpp:88–134  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

86 }
87
88 bool raiseEvent(const std::vector<script::Value>& event) override {
89 // return eval("if (typeof onEvent === \"function\") onEvent(\"" + event + "\");");
90 bool success = true;
91 try {
92 v8::Isolate::Scope isolatescope(m_isolate);
93 // Create a stack-allocated handle scope.
94 v8::HandleScope handle_scope(m_isolate);
95
96 // Enter the context for compiling and running the hello world script.
97 v8::Context::Scope context_scope(context());
98
99 v8::TryCatch trycatch(m_isolate);
100
101 auto global = context()->Global();
102
103 auto onEvent = ToLocal(global->Get(context(), ToLocal(v8::String::NewFromUtf8(m_isolate, "onEvent"))));
104 if (!onEvent.IsEmpty() && onEvent->IsFunction()) {
105 std::vector<v8::Local<v8::Value>> argv;
106 argv.reserve(event.size());
107 for (auto& arg : event) {
108 argv.emplace_back(returnValue(m_isolate, arg));
109 }
110 Check(onEvent.As<v8::Function>()->Call(context(), global, event.size(), argv.data()));
111 }
112
113 if (trycatch.HasCaught()) {
114 v8::Local<v8::Value> exception = trycatch.Exception();
115 auto trace = trycatch.StackTrace(context());
116
117 v8::String::Utf8Value utf8(m_isolate, exception);
118
119 if (!trace.IsEmpty()){
120 v8::String::Utf8Value utf8Trace(m_isolate, ToLocal(trace));
121 m_delegate->onConsolePrint(*utf8Trace);
122 } else {
123 m_delegate->onConsolePrint(*utf8);
124 }
125 success = false;
126 }
127
128 } catch (const std::exception& ex) {
129 m_delegate->onConsolePrint(ex.what());
130 success = false;
131 }
132 execAfterEval(success);
133 return success;
134 }
135
136 bool eval(const std::string& code) override {
137 bool success = true;

Callers

nothing calls this directly

Calls 9

ToLocalFunction · 0.85
CheckFunction · 0.85
reserveMethod · 0.80
ExceptionMethod · 0.80
whatMethod · 0.80
returnValueFunction · 0.70
sizeMethod · 0.45
dataMethod · 0.45
onConsolePrintMethod · 0.45

Tested by

no test coverage detected