v8 :: Script :: Compile (v8 :: String :: New (& quot; .make.some.syntax.errors & quot;), v8 :: String :: New (& quot; main & quot;)) — & gt; Run () вызывает ошибка сегментации

Допустим, у меня есть этот код:

Local<Script> script = Script::Compile(String::New("x1 = 1;"), String::New("main.js"));
printf("before run\n");
script->Run();
printf("after run\n");

Контекст был создан и введен ранее.
Выход этого кода:

before run
after run

Что, как и ожидалось. Но если положить некоторый код JavaScript в source который содержит синтаксические ошибки (например, ".x11 = 1") затем вывод:

main.js:0: Uncaught SyntaxError: Unexpected token .
before execution.
Segmentation fault (core dumped)

Может мне не стоит звонить Run если при компиляции есть ошибки, но как это проверить?

Кроме того: (код из Получение цели — Chrome V8 + код с синтаксическими ошибками = тоже самое)

#include <v8.h>

using namespace v8;

int main(int argc, char* argv[]) {

// Get the default Isolate created at startup.

Isolate* isolate = Isolate::GetCurrent();

// Create a stack-allocated handle scope.

HandleScope handle_scope(isolate);

// Create a new context.

Handle<Context> context = Context::New(isolate);

// Here's how you could create a Persistent handle to the context, if needed.

Persistent<Context> persistent_context(isolate, context);// Enter the created context for compiling and

// running the hello world script.

Context::Scope context_scope(context);

// Create a string containing the JavaScript source code.

Handle<String> source = String::New(".>make.some.syntax.errors<");

// Compile the source code.

Handle<Script> script = Script::Compile(source);

// Run the script to get the result.

Handle<Value> result = script->Run();

// The persistent handle needs to be eventually disposed.

persistent_context.Dispose();

// Convert the result to an ASCII string and print it.

String::AsciiValue ascii(result);

printf("%s\n", *ascii);

return 0;

}

3

Решение

После некоторой головной боли я нашел способ обойти это.

При сбое компиляции скрипта он ничего не возвращает v8::Handle, Итак, в этом случае, script.IsEmpty() возвращает истину.

Чтобы сделать это понятным, я сделал некоторые изменения в коде Hello World Google:

#include <v8.h>

using namespace v8;

int main(int argc, char* argv[]) {

// Get the default Isolate created at startup.

Isolate* isolate = Isolate::GetCurrent();

// Create a stack-allocated handle scope.

HandleScope handle_scope(isolate);

// Create a new context.

Handle<Context> context = Context::New(isolate);

// Here's how you could create a Persistent handle to the context, if needed.

Persistent<Context> persistent_context(isolate, context);// Enter the created context for compiling and

// running the hello world script.

Context::Scope context_scope(context);

// Create a string containing the JavaScript source code.

Handle<String> source = String::New(".>no.matter.what.code.is<");

// Compile the source code.

Handle<Script> script = Script::Compile(source);
if(!script.IsEmpty()) // is script compiled ?
{
// Run the script to get the result.

Handle<Value> result = script->Run();

// Convert the result to an ASCII string and print it.
String::AsciiValue ascii(result);

printf("%s\n", *ascii);
}

// The persistent handle needs to be eventually disposed.

persistent_context.Dispose();

return 0;

}
1

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector