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

Method eval

src/script/v8/engine.cpp:136–191  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

134 }
135
136 bool eval(const std::string& code) override {
137 bool success = true;
138 try {
139 v8::Isolate::Scope isolatescope(m_isolate);
140 // Create a stack-allocated handle scope.
141 v8::HandleScope handle_scope(m_isolate);
142
143 // Enter the context for compiling and running the hello world script.
144 v8::Context::Scope context_scope(context());
145
146 v8::TryCatch trycatch(m_isolate);
147
148 initGlobals();
149
150 // Create a string containing the JavaScript source code.
151 v8::Local<v8::String> source = ToLocal(v8::String::NewFromUtf8(m_isolate, code.c_str()));
152
153 // Compile the source code.
154 v8::MaybeLocal<v8::Script> script = v8::Script::Compile(context(), source);
155 // Run the script to get the result.
156 v8::MaybeLocal<v8::Value> result;
157 if (!script.IsEmpty()) {
158 result = ToLocal(script)->Run(context());
159 }
160
161 if (result.IsEmpty()) {
162 if (trycatch.HasCaught()) {
163 v8::Local<v8::Value> exception = trycatch.Exception();
164 auto trace = trycatch.StackTrace(context());
165
166 v8::String::Utf8Value utf8(m_isolate, exception);
167 m_delegate->onConsolePrint(*utf8);
168
169 if (!trace.IsEmpty()){
170 v8::String::Utf8Value utf8Trace(m_isolate, ToLocal(trace));
171 m_delegate->onConsolePrint(*utf8Trace);
172 }
173
174 std::cout << "Error: [" << *utf8 << "]" << std::endl;
175 success = false;
176 }
177 } else if (m_printLastResult) {
178 v8::String::Utf8Value utf8(m_isolate, result.ToLocalChecked());
179 m_delegate->onConsolePrint(*utf8);
180 }
181 m_isolate->LowMemoryNotification();
182 } catch (const std::exception& ex) {
183 std::string err = "Error: ";
184 err += ex.what();
185 m_delegate->onConsolePrint(err.c_str());
186 success = false;
187 std::cout << "Error: [" << err << "]" << std::endl;
188 }
189 execAfterEval(success);
190 return success;
191 }
192};
193

Callers

nothing calls this directly

Calls 4

ToLocalFunction · 0.85
ExceptionMethod · 0.80
whatMethod · 0.80
onConsolePrintMethod · 0.45

Tested by

no test coverage detected