From bee0ee92e7f99ed46140fa9f611d375a9a668ce2 Mon Sep 17 00:00:00 2001 From: Nicolas Vivet Date: Fri, 26 Jun 2020 17:13:13 +0200 Subject: [PATCH 1/6] Introduce a strict mode --- py_mini_racer/py_mini_racer.py | 103 ++++++++++++++++++++++++--------- tests/test_strict.py | 34 +++++++++++ 2 files changed, 111 insertions(+), 26 deletions(-) create mode 100644 tests/test_strict.py diff --git a/py_mini_racer/py_mini_racer.py b/py_mini_racer/py_mini_racer.py index 50aeb58c..c969fbf7 100644 --- a/py_mini_racer/py_mini_racer.py +++ b/py_mini_racer/py_mini_racer.py @@ -175,8 +175,7 @@ def eval(self, js_str, timeout=0, max_memory=0): if bool(res) is False: raise JSConversionException() - python_value = res.contents.to_python() - return python_value + return self._eval_return(res) finally: self.lock.release() if res is not None: @@ -226,6 +225,42 @@ def __del__(self): self.ext.mr_free_context(self.ctx) + @staticmethod + def _eval_return(res): + return res.contents.to_python() + + +class StrictMiniRacer(MiniRacer): + """ + A stricter version of MiniRacer accepting only scalars as a return value + (boolean, integer, strings, ...). + """ + + json_impl = json + + def execute(self, expr, **kwargs): + """ Stricter Execute with JSON serialization of returned value. + """ + wrapped_expr = "JSON.stringify((function(){return (%s)})())" % expr + ret = self.eval(wrapped_expr, **kwargs) + if isinstance(ret, str): + return self.json_impl.loads(ret) + + def call(self, identifier, *args, **kwargs): + """ Stricter Call with JSON serialization of returned value. + """ + json_args = self.json_impl.dumps(args, separators=(',', ':'), + cls=kwargs.get("encoder")) + js = "JSON.stringify({identifier}.apply(this, {json_args}))" + ret = self.eval(js.format(identifier=identifier, json_args=json_args), + **kwargs) + if isinstance(ret, str): + return self.json_impl.loads(ret) + + @staticmethod + def _eval_return(res): + return res.contents.scalar_to_python() + class PythonTypes(object): """ Python types identifier - need to be coherent with @@ -263,9 +298,29 @@ def _double_value(self): ptr = ctypes.c_char_p.from_buffer(self) return ctypes.c_double.from_buffer(ptr).value - def to_python(self): - """ Return an object as native Python """ + def _raise_from_error(self): + if self.type == PythonTypes.parse_exception: + msg = ctypes.c_char_p(self.value).value + raise JSParseException(msg) + elif self.type == PythonTypes.execute_exception: + msg = ctypes.c_char_p(self.value).value + raise JSEvalException(msg.decode('utf-8', errors='replace')) + elif self.type == PythonTypes.oom_exception: + msg = ctypes.c_char_p(self.value).value + raise JSOOMException(msg) + elif self.type == PythonTypes.timeout_exception: + msg = ctypes.c_char_p(self.value).value + raise JSTimeoutException(msg) + + def string_to_python(self, **kwargs): + self._raise_from_error() + if self.type != PythonTypes.str_utf8: + raise WrongReturnTypeException( + "returned value is not a unicode string") + return self.scalar_to_python() + def scalar_to_python(self): + self._raise_from_error() result = None if self.type == PythonTypes.null: result = None @@ -282,7 +337,23 @@ def to_python(self): buf = ctypes.c_char_p(self.value) ptr = ctypes.cast(buf, ctypes.POINTER(ctypes.c_char)) result = ptr[0:self.len].decode("utf8") - elif self.type == PythonTypes.array: + elif self.type == PythonTypes.function: + result = JSFunction() + elif self.type == PythonTypes.date: + timestamp = self._double_value() + # JS timestamp are milliseconds, in python we are in seconds + result = datetime.datetime.utcfromtimestamp(timestamp / 1000.) + elif self.type == PythonTypes.symbol: + result = JSSymbol() + else: + raise WrongReturnTypeException("unknown type %d" % self.type) + return result + + def to_python(self): + """ Return an object as native Python """ + self._raise_from_error() + result = None + if self.type == PythonTypes.array: if self.len == 0: return [] ary = [] @@ -303,26 +374,6 @@ def to_python(self): pval = PythonValue.from_address(ptr_to_hash[i*2+1]) res[pkey.to_python()] = pval.to_python() result = res - elif self.type == PythonTypes.function: - result = JSFunction() - elif self.type == PythonTypes.parse_exception: - msg = ctypes.c_char_p(self.value).value - raise JSParseException(msg) - elif self.type == PythonTypes.execute_exception: - msg = ctypes.c_char_p(self.value).value - raise JSEvalException(msg.decode('utf-8', errors='replace')) - elif self.type == PythonTypes.oom_exception: - msg = ctypes.c_char_p(self.value).value - raise JSOOMException(msg) - elif self.type == PythonTypes.timeout_exception: - msg = ctypes.c_char_p(self.value).value - raise JSTimeoutException(msg) - elif self.type == PythonTypes.date: - timestamp = self._double_value() - # JS timestamp are milliseconds, in python we are in seconds - result = datetime.datetime.utcfromtimestamp(timestamp / 1000.) - elif self.type == PythonTypes.symbol: - result = JSSymbol() else: - raise WrongReturnTypeException("unknown type %d" % self.type) + result = self.scalar_to_python() return result diff --git a/tests/test_strict.py b/tests/test_strict.py new file mode 100644 index 00000000..ab6cb963 --- /dev/null +++ b/tests/test_strict.py @@ -0,0 +1,34 @@ +import unittest + +from py_mini_racer import py_mini_racer + + +class StrictTestCase(unittest.TestCase): + """Test StrictMiniRacer""" + + def setUp(self): + self.mr = py_mini_racer.StrictMiniRacer() + + def test_basic_int(self): + self.assertEqual(42, self.mr.execute("42")) + + def test_basic_string(self): + self.assertEqual("42", self.mr.execute('"42"')) + + def test_basic_hash(self): + self.assertEqual({}, self.mr.execute('{}')) + + def test_basic_array(self): + self.assertEqual([1, 2, 3], self.mr.execute('[1, 2, 3]')) + + def test_not_allowed_type(self): + with self.assertRaises(py_mini_racer.WrongReturnTypeException): + self.mr.eval("Object()") + + def test_call(self): + js_func = """var f = function(args) { + return args.length; + }""" + + self.assertIsNone(self.mr.eval(js_func)) + self.assertEqual(self.mr.call('f', list(range(5))), 5) From 870f5c8667fcdb6c82fe31c30671ee13c332884a Mon Sep 17 00:00:00 2001 From: Nicolas Vivet Date: Mon, 29 Jun 2020 09:21:55 +0200 Subject: [PATCH 2/6] Test unicode --- py_mini_racer/py_mini_racer.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/py_mini_racer/py_mini_racer.py b/py_mini_racer/py_mini_racer.py index c969fbf7..4df31cd1 100644 --- a/py_mini_racer/py_mini_racer.py +++ b/py_mini_racer/py_mini_racer.py @@ -23,6 +23,12 @@ EXTENSION_NAME = fnmatch.filter(os.listdir(__location__), '_v8*.so')[0] EXTENSION_PATH = os.path.join(__location__, EXTENSION_NAME) +if sys.version_info[0] < 3: + UNICODE_TYPE = unicode +else: + UNICODE_TYPE = str + + class MiniRacerBaseException(Exception): """ base MiniRacer exception class """ pass @@ -59,6 +65,7 @@ class JSSymbol(object): """ type for JS symbols """ pass + def is_unicode(value): """ Check if a value is a valid unicode string, compatible with python 2 and python 3 @@ -73,14 +80,7 @@ def is_unicode(value): >>> is_unicode(('abc',)) False """ - python_version = sys.version_info[0] - - if python_version == 2: - return isinstance(value, unicode) - elif python_version == 3: - return isinstance(value, str) - else: - raise NotImplementedError() + return isinstance(value, UNICODE_TYPE) _ext_handle = None @@ -243,7 +243,7 @@ def execute(self, expr, **kwargs): """ wrapped_expr = "JSON.stringify((function(){return (%s)})())" % expr ret = self.eval(wrapped_expr, **kwargs) - if isinstance(ret, str): + if is_unicode(ret): return self.json_impl.loads(ret) def call(self, identifier, *args, **kwargs): @@ -254,7 +254,7 @@ def call(self, identifier, *args, **kwargs): js = "JSON.stringify({identifier}.apply(this, {json_args}))" ret = self.eval(js.format(identifier=identifier, json_args=json_args), **kwargs) - if isinstance(ret, str): + if is_unicode(ret): return self.json_impl.loads(ret) @staticmethod From 16f20c15db4b2b44bf618ed0621ec1b137d28c35 Mon Sep 17 00:00:00 2001 From: Nicolas Vivet Date: Mon, 29 Jun 2020 11:58:39 +0200 Subject: [PATCH 3/6] Use basic instead of scalar --- .../extension/mini_racer_extension.cc | 110 +++++++++++------- py_mini_racer/py_mini_racer.py | 28 +++-- tests/test_strict.py | 2 +- 3 files changed, 85 insertions(+), 55 deletions(-) diff --git a/py_mini_racer/extension/mini_racer_extension.cc b/py_mini_racer/extension/mini_racer_extension.cc index 52b064b4..cdb46e1a 100644 --- a/py_mini_racer/extension/mini_racer_extension.cc +++ b/py_mini_racer/extension/mini_racer_extension.cc @@ -163,6 +163,7 @@ typedef struct { unsigned long timeout; EvalResult* result; size_t max_memory; + bool basic_only; } EvalParams; enum IsolateData { @@ -413,11 +414,11 @@ static BinaryValue *heap_stats(ContextInfo *context_info) { } -static BinaryValue *convert_v8_to_binary(Isolate * isolate, - Local context, - Local value) +static BinaryValue *convert_basic_v8_to_binary(Isolate * isolate, + Local context, + Local value) { - Isolate::Scope isolate_scope(isolate); + Isolate::Scope isolate_scope(isolate); HandleScope scope(isolate); BinaryValue *res = new (xalloc(res)) BinaryValue(); @@ -425,13 +426,11 @@ static BinaryValue *convert_v8_to_binary(Isolate * isolate, if (value->IsNull() || value->IsUndefined()) { res->type = type_null; } - else if (value->IsInt32()) { res->type = type_integer; auto val = value->Uint32Value(context).ToChecked(); res->int_val = val; } - // ECMA-262, 4.3.20 // http://www.ecma-international.org/ecma-262/5.1/#sec-4.3.19 else if (value->IsNumber()) { @@ -439,13 +438,56 @@ static BinaryValue *convert_v8_to_binary(Isolate * isolate, double val = value->NumberValue(context).ToChecked(); res->double_val = val; } - else if (value->IsBoolean()) { res->type = type_bool; res->int_val = (value->IsTrue() ? 1 : 0); } + else if (value->IsFunction()){ + res->type = type_function; + } + else if (value->IsSymbol()){ + res->type = type_symbol; + } + else if (value->IsDate()) { + res->type = type_date; + Local date = Local::Cast(value); + + double timestamp = date->ValueOf(); + res->double_val = timestamp; + } + else if (value->IsString()) { + Local rstr = value->ToString(context).ToLocalChecked(); + + res->type = type_str_utf8; + res->len = size_t(rstr->Utf8Length(isolate)); // in bytes + size_t capacity = res->len + 1; + res->str_val = xalloc(res->str_val, capacity); + rstr->WriteUtf8(isolate, res->str_val); + } + else { + BinaryValueFree(res); + res = nullptr; + } + return res; +} + + +static BinaryValue *convert_v8_to_binary(Isolate * isolate, + Local context, + Local value) +{ + Isolate::Scope isolate_scope(isolate); + HandleScope scope(isolate); + BinaryValue *res; - else if (value->IsArray()) { + res = convert_basic_v8_to_binary(isolate, context, value); + if (res) { + return res; + } + + res = new (xalloc(res)) BinaryValue(); + + if (value->IsArray()) { Local arr = Local::Cast(value); uint32_t len = arr->Length(); @@ -466,23 +508,6 @@ static BinaryValue *convert_v8_to_binary(Isolate * isolate, ary[i] = bin_value; } } - - else if (value->IsFunction()){ - res->type = type_function; - } - - else if (value->IsSymbol()){ - res->type = type_symbol; - } - - else if (value->IsDate()) { - res->type = type_date; - Local date = Local::Cast(value); - - double timestamp = date->ValueOf(); - res->double_val = timestamp; - } - else if (value->IsObject()) { res->type = type_hash; @@ -528,16 +553,8 @@ static BinaryValue *convert_v8_to_binary(Isolate * isolate, res->len++; } } // else empty hash - } - - else { - Local rstr = value->ToString(context).ToLocalChecked(); - - res->type = type_str_utf8; - res->len = size_t(rstr->Utf8Length(isolate)); // in bytes - size_t capacity = res->len + 1; - res->str_val = xalloc(res->str_val, capacity); - rstr->WriteUtf8(isolate, res->str_val); + } else { + goto err; } return res; @@ -547,6 +564,16 @@ static BinaryValue *convert_v8_to_binary(Isolate * isolate, } +static BinaryValue *convert_basic_v8_to_binary(Isolate * isolate, + const Persistent & context, + Local value) +{ + HandleScope scope(isolate); + return convert_basic_v8_to_binary(isolate, + Local::New(isolate, context), + value); +} + static BinaryValue *convert_v8_to_binary(Isolate * isolate, const Persistent & context, Local value) @@ -610,7 +637,7 @@ ContextInfo *MiniRacer_init_context() static BinaryValue* MiniRacer_eval_context_unsafe( ContextInfo *context_info, char *utf_str, int str_len, - unsigned long timeout, size_t max_memory) + unsigned long timeout, size_t max_memory, bool basic_only) { EvalParams eval_params; EvalResult eval_result{}; @@ -643,6 +670,7 @@ static BinaryValue* MiniRacer_eval_context_unsafe( eval_params.result = &eval_result; eval_params.timeout = 0; eval_params.max_memory = 0; + eval_params.basic_only = basic_only; if (timeout > 0) { eval_params.timeout = timeout; } @@ -732,7 +760,11 @@ static BinaryValue* MiniRacer_eval_context_unsafe( HandleScope handle_scope(context_info->isolate); Local tmp = Local::New(context_info->isolate, *eval_result.value); - result = convert_v8_to_binary(context_info->isolate, *context_info->context, tmp); + if (eval_params.basic_only) { + result = convert_basic_v8_to_binary(context_info->isolate, *context_info->context, tmp); + } else { + result = convert_v8_to_binary(context_info->isolate, *context_info->context, tmp); + } } BinaryValueFree(bmessage); @@ -767,8 +799,8 @@ class BufferOutputStream: public OutputStream { extern "C" { -LIB_EXPORT BinaryValue * mr_eval_context(ContextInfo *context_info, char *str, int len, unsigned long timeout, size_t max_memory) { - BinaryValue *res = MiniRacer_eval_context_unsafe(context_info, str, len, timeout, max_memory); +LIB_EXPORT BinaryValue * mr_eval_context(ContextInfo *context_info, char *str, int len, unsigned long timeout, size_t max_memory, bool basic_only) { + BinaryValue *res = MiniRacer_eval_context_unsafe(context_info, str, len, timeout, max_memory, basic_only); return res; } diff --git a/py_mini_racer/py_mini_racer.py b/py_mini_racer/py_mini_racer.py index 4df31cd1..84f9d22a 100644 --- a/py_mini_racer/py_mini_racer.py +++ b/py_mini_racer/py_mini_racer.py @@ -101,7 +101,8 @@ def _fetch_ext_handle(): ctypes.c_char_p, ctypes.c_int, ctypes.c_ulong, - ctypes.c_size_t] + ctypes.c_size_t, + ctypes.c_bool] _ext_handle.mr_eval_context.restype = ctypes.POINTER(PythonValue) _ext_handle.mr_free_value.argtypes = [ctypes.c_void_p] @@ -130,6 +131,8 @@ class MiniRacer(object): https://docs.python.org/2/library/ctypes.html """ + basic_types_only = False + def __init__(self): """ Init a JS context """ @@ -171,7 +174,8 @@ def eval(self, js_str, timeout=0, max_memory=0): bytes_val, len(bytes_val), ctypes.c_ulong(timeout), - ctypes.c_size_t(max_memory)) + ctypes.c_size_t(max_memory), + ctypes.c_bool(self.basic_types_only)) if bool(res) is False: raise JSConversionException() @@ -232,11 +236,12 @@ def _eval_return(res): class StrictMiniRacer(MiniRacer): """ - A stricter version of MiniRacer accepting only scalars as a return value - (boolean, integer, strings, ...). + A stricter version of MiniRacer accepting only basic types as a return value + (boolean, integer, strings, ...), array and mapping are disallowed. """ json_impl = json + basic_types_only = True def execute(self, expr, **kwargs): """ Stricter Execute with JSON serialization of returned value. @@ -259,7 +264,7 @@ def call(self, identifier, *args, **kwargs): @staticmethod def _eval_return(res): - return res.contents.scalar_to_python() + return res.contents.basic_to_python() class PythonTypes(object): @@ -312,14 +317,7 @@ def _raise_from_error(self): msg = ctypes.c_char_p(self.value).value raise JSTimeoutException(msg) - def string_to_python(self, **kwargs): - self._raise_from_error() - if self.type != PythonTypes.str_utf8: - raise WrongReturnTypeException( - "returned value is not a unicode string") - return self.scalar_to_python() - - def scalar_to_python(self): + def basic_to_python(self): self._raise_from_error() result = None if self.type == PythonTypes.null: @@ -346,7 +344,7 @@ def scalar_to_python(self): elif self.type == PythonTypes.symbol: result = JSSymbol() else: - raise WrongReturnTypeException("unknown type %d" % self.type) + raise JSConversionException() return result def to_python(self): @@ -375,5 +373,5 @@ def to_python(self): res[pkey.to_python()] = pval.to_python() result = res else: - result = self.scalar_to_python() + result = self.basic_to_python() return result diff --git a/tests/test_strict.py b/tests/test_strict.py index ab6cb963..8e643be8 100644 --- a/tests/test_strict.py +++ b/tests/test_strict.py @@ -22,7 +22,7 @@ def test_basic_array(self): self.assertEqual([1, 2, 3], self.mr.execute('[1, 2, 3]')) def test_not_allowed_type(self): - with self.assertRaises(py_mini_racer.WrongReturnTypeException): + with self.assertRaises(py_mini_racer.JSConversionException): self.mr.eval("Object()") def test_call(self): From 7631fa5055386f34d9f624bcf7c4d0e76796b1db Mon Sep 17 00:00:00 2001 From: Nicolas Vivet Date: Mon, 29 Jun 2020 12:17:36 +0200 Subject: [PATCH 4/6] Do not allow complex types in error messages --- py_mini_racer/extension/mini_racer_extension.cc | 8 ++++++-- tests/test_strict.py | 4 ++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/py_mini_racer/extension/mini_racer_extension.cc b/py_mini_racer/extension/mini_racer_extension.cc index cdb46e1a..266d063c 100644 --- a/py_mini_racer/extension/mini_racer_extension.cc +++ b/py_mini_racer/extension/mini_racer_extension.cc @@ -684,14 +684,18 @@ static BinaryValue* MiniRacer_eval_context_unsafe( Local tmp = Local::New(context_info->isolate, *eval_result.message); - bmessage = convert_v8_to_binary(context_info->isolate, *context_info->context, tmp); + if (eval_params.basic_only) { + bmessage = convert_basic_v8_to_binary(context_info->isolate, *context_info->context, tmp); + } else { + bmessage = convert_v8_to_binary(context_info->isolate, *context_info->context, tmp); + } } if (eval_result.backtrace) { Local tmp = Local::New(context_info->isolate, *eval_result.backtrace); - bbacktrace = convert_v8_to_binary(context_info->isolate, *context_info->context, tmp); + bbacktrace = convert_basic_v8_to_binary(context_info->isolate, *context_info->context, tmp); } } diff --git a/tests/test_strict.py b/tests/test_strict.py index 8e643be8..c49c778b 100644 --- a/tests/test_strict.py +++ b/tests/test_strict.py @@ -32,3 +32,7 @@ def test_call(self): self.assertIsNone(self.mr.eval(js_func)) self.assertEqual(self.mr.call('f', list(range(5))), 5) + + def test_message(self): + with self.assertRaises(py_mini_racer.JSEvalException): + res = self.mr.eval("throw new EvalError('Hello', 'someFile.js', 10);") From 645c22b3ef82875efd0080cb539f40e7b88de3b6 Mon Sep 17 00:00:00 2001 From: Nicolas Vivet Date: Mon, 29 Jun 2020 14:13:15 +0200 Subject: [PATCH 5/6] Formatting --- .../extension/mini_racer_extension.cc | 71 ++++++++++--------- 1 file changed, 36 insertions(+), 35 deletions(-) diff --git a/py_mini_racer/extension/mini_racer_extension.cc b/py_mini_racer/extension/mini_racer_extension.cc index 266d063c..741a410a 100644 --- a/py_mini_racer/extension/mini_racer_extension.cc +++ b/py_mini_racer/extension/mini_racer_extension.cc @@ -275,30 +275,30 @@ static void* nogvl_context_eval(void* arg) { if (trycatch.HasCaught()) { if (!trycatch.Exception()->IsNull()) { result->message = new Persistent(); - Local message = trycatch.Message(); - char buf[1000]; - int len, line, column; - - if (!message->GetLineNumber(context).To(&line)) { - line = 0; - } - - if (!message->GetStartColumn(context).To(&column)) { - column = 0; - } - - len = snprintf(buf, sizeof(buf), "%s at %s:%i:%i", *String::Utf8Value(isolate, message->Get()), - *String::Utf8Value(isolate, message->GetScriptResourceName()->ToString(context).ToLocalChecked()), - line, - column); - - if ((size_t) len >= sizeof(buf)) { - len = sizeof(buf) - 1; - buf[len] = '\0'; - } - - Local v8_message = String::NewFromUtf8(isolate, buf, NewStringType::kNormal, len).ToLocalChecked(); - result->message->Reset(isolate, v8_message); + Local message = trycatch.Message(); + char buf[1000]; + int len, line, column; + + if (!message->GetLineNumber(context).To(&line)) { + line = 0; + } + + if (!message->GetStartColumn(context).To(&column)) { + column = 0; + } + + len = snprintf(buf, sizeof(buf), "%s at %s:%i:%i", *String::Utf8Value(isolate, message->Get()), + *String::Utf8Value(isolate, message->GetScriptResourceName()->ToString(context).ToLocalChecked()), + line, + column); + + if ((size_t) len >= sizeof(buf)) { + len = sizeof(buf) - 1; + buf[len] = '\0'; + } + + Local v8_message = String::NewFromUtf8(isolate, buf, NewStringType::kNormal, len).ToLocalChecked(); + result->message->Reset(isolate, v8_message); } else if(trycatch.HasTerminated()) { result->terminated = true; result->message = new Persistent(); @@ -312,6 +312,7 @@ static void* nogvl_context_eval(void* arg) { } if (!trycatch.StackTrace(context).IsEmpty()) { + result->backtrace = new Persistent(); result->backtrace->Reset(isolate, trycatch.StackTrace(context).ToLocalChecked()->ToString(context).ToLocalChecked()); } @@ -387,7 +388,7 @@ static BinaryValue *heap_stats(ContextInfo *context_info) { content[idx++ * 2 + 1] = new_bv_int(0); content[idx++ * 2 + 1] = new_bv_int(0); } else { - isolate->GetHeapStatistics(&stats); + isolate->GetHeapStatistics(&stats); content[idx++ * 2 + 1] = new_bv_int(stats.total_physical_size()); content[idx++ * 2 + 1] = new_bv_int(stats.total_heap_size_executable()); @@ -415,7 +416,7 @@ static BinaryValue *heap_stats(ContextInfo *context_info) { static BinaryValue *convert_basic_v8_to_binary(Isolate * isolate, - Local context, + Local context, Local value) { Isolate::Scope isolate_scope(isolate); @@ -528,9 +529,9 @@ static BinaryValue *convert_v8_to_binary(Isolate * isolate, MaybeLocal maybe_pkey = props->Get(context, i); if (maybe_pkey.IsEmpty()) { - goto err; - } - Local pkey = maybe_pkey.ToLocalChecked(); + goto err; + } + Local pkey = maybe_pkey.ToLocalChecked(); MaybeLocal maybe_pvalue = object->Get(context, pkey); // this may have failed due to Get raising if (maybe_pvalue.IsEmpty() || trycatch.HasCaught()) { @@ -575,7 +576,7 @@ static BinaryValue *convert_basic_v8_to_binary(Isolate * isolate, } static BinaryValue *convert_v8_to_binary(Isolate * isolate, - const Persistent & context, + const Persistent & context, Local value) { HandleScope scope(isolate); @@ -686,9 +687,9 @@ static BinaryValue* MiniRacer_eval_context_unsafe( if (eval_params.basic_only) { bmessage = convert_basic_v8_to_binary(context_info->isolate, *context_info->context, tmp); - } else { + } else { bmessage = convert_v8_to_binary(context_info->isolate, *context_info->context, tmp); - } + } } if (eval_result.backtrace) { @@ -764,11 +765,11 @@ static BinaryValue* MiniRacer_eval_context_unsafe( HandleScope handle_scope(context_info->isolate); Local tmp = Local::New(context_info->isolate, *eval_result.value); - if (eval_params.basic_only) { + if (eval_params.basic_only) { result = convert_basic_v8_to_binary(context_info->isolate, *context_info->context, tmp); - } else { + } else { result = convert_v8_to_binary(context_info->isolate, *context_info->context, tmp); - } + } } BinaryValueFree(bmessage); From 955427b2dbf1095147ac646f1444b213e6200991 Mon Sep 17 00:00:00 2001 From: Nicolas Vivet Date: Mon, 29 Jun 2020 14:31:56 +0200 Subject: [PATCH 6/6] Remove some assertions --- .../extension/mini_racer_extension.cc | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/py_mini_racer/extension/mini_racer_extension.cc b/py_mini_racer/extension/mini_racer_extension.cc index 741a410a..52a93411 100644 --- a/py_mini_racer/extension/mini_racer_extension.cc +++ b/py_mini_racer/extension/mini_racer_extension.cc @@ -312,15 +312,25 @@ static void* nogvl_context_eval(void* arg) { } if (!trycatch.StackTrace(context).IsEmpty()) { + Local stacktrace; - result->backtrace = new Persistent(); - result->backtrace->Reset(isolate, trycatch.StackTrace(context).ToLocalChecked()->ToString(context).ToLocalChecked()); + if (trycatch.StackTrace(context).ToLocal(&stacktrace)) { + Local tmp; + + if (stacktrace->ToString(context).ToLocal(&tmp)) { + result->backtrace = new Persistent(); + result->backtrace->Reset(isolate, tmp); + } + } } } } else { - Persistent* persistent = new Persistent(); - persistent->Reset(isolate, maybe_value.ToLocalChecked()); - result->value = persistent; + Local tmp; + + if (maybe_value.ToLocal(&tmp)) { + result->value = new Persistent(); + result->value->Reset(isolate, tmp); + } } } @@ -759,7 +769,7 @@ static BinaryValue* MiniRacer_eval_context_unsafe( } } - else { + else if (eval_result.value) { Locker lock(context_info->isolate); Isolate::Scope isolate_scope(context_info->isolate); HandleScope handle_scope(context_info->isolate); @@ -857,3 +867,5 @@ LIB_EXPORT BinaryValue * mr_heap_snapshot(ContextInfo *context_info) { return bos.bv; } } + +// vim: set shiftwidth=4 softtabstop=4 expandtab: