@@ -163,6 +163,7 @@ typedef struct {
163163 unsigned long timeout;
164164 EvalResult* result;
165165 size_t max_memory;
166+ bool basic_only;
166167} EvalParams;
167168
168169enum IsolateData {
@@ -413,39 +414,80 @@ static BinaryValue *heap_stats(ContextInfo *context_info) {
413414}
414415
415416
416- static BinaryValue *convert_v8_to_binary (Isolate * isolate,
417- Local<Context> context,
418- Local<Value> value)
417+ static BinaryValue *convert_basic_v8_to_binary (Isolate * isolate,
418+ Local<Context> context,
419+ Local<Value> value)
419420{
420- Isolate::Scope isolate_scope (isolate);
421+ Isolate::Scope isolate_scope (isolate);
421422 HandleScope scope (isolate);
422423
423424 BinaryValue *res = new (xalloc (res)) BinaryValue ();
424425
425426 if (value->IsNull () || value->IsUndefined ()) {
426427 res->type = type_null;
427428 }
428-
429429 else if (value->IsInt32 ()) {
430430 res->type = type_integer;
431431 auto val = value->Uint32Value (context).ToChecked ();
432432 res->int_val = val;
433433 }
434-
435434 // ECMA-262, 4.3.20
436435 // http://www.ecma-international.org/ecma-262/5.1/#sec-4.3.19
437436 else if (value->IsNumber ()) {
438437 res->type = type_double;
439438 double val = value->NumberValue (context).ToChecked ();
440439 res->double_val = val;
441440 }
442-
443441 else if (value->IsBoolean ()) {
444442 res->type = type_bool;
445443 res->int_val = (value->IsTrue () ? 1 : 0 );
446444 }
445+ else if (value->IsFunction ()){
446+ res->type = type_function;
447+ }
448+ else if (value->IsSymbol ()){
449+ res->type = type_symbol;
450+ }
451+ else if (value->IsDate ()) {
452+ res->type = type_date;
453+ Local<Date> date = Local<Date>::Cast (value);
454+
455+ double timestamp = date->ValueOf ();
456+ res->double_val = timestamp;
457+ }
458+ else if (value->IsString ()) {
459+ Local<String> rstr = value->ToString (context).ToLocalChecked ();
460+
461+ res->type = type_str_utf8;
462+ res->len = size_t (rstr->Utf8Length (isolate)); // in bytes
463+ size_t capacity = res->len + 1 ;
464+ res->str_val = xalloc (res->str_val , capacity);
465+ rstr->WriteUtf8 (isolate, res->str_val );
466+ }
467+ else {
468+ BinaryValueFree (res);
469+ res = nullptr ;
470+ }
471+ return res;
472+ }
473+
474+
475+ static BinaryValue *convert_v8_to_binary (Isolate * isolate,
476+ Local<Context> context,
477+ Local<Value> value)
478+ {
479+ Isolate::Scope isolate_scope (isolate);
480+ HandleScope scope (isolate);
481+ BinaryValue *res;
447482
448- else if (value->IsArray ()) {
483+ res = convert_basic_v8_to_binary (isolate, context, value);
484+ if (res) {
485+ return res;
486+ }
487+
488+ res = new (xalloc (res)) BinaryValue ();
489+
490+ if (value->IsArray ()) {
449491 Local<Array> arr = Local<Array>::Cast (value);
450492 uint32_t len = arr->Length ();
451493
@@ -466,23 +508,6 @@ static BinaryValue *convert_v8_to_binary(Isolate * isolate,
466508 ary[i] = bin_value;
467509 }
468510 }
469-
470- else if (value->IsFunction ()){
471- res->type = type_function;
472- }
473-
474- else if (value->IsSymbol ()){
475- res->type = type_symbol;
476- }
477-
478- else if (value->IsDate ()) {
479- res->type = type_date;
480- Local<Date> date = Local<Date>::Cast (value);
481-
482- double timestamp = date->ValueOf ();
483- res->double_val = timestamp;
484- }
485-
486511 else if (value->IsObject ()) {
487512 res->type = type_hash;
488513
@@ -528,16 +553,8 @@ static BinaryValue *convert_v8_to_binary(Isolate * isolate,
528553 res->len ++;
529554 }
530555 } // else empty hash
531- }
532-
533- else {
534- Local<String> rstr = value->ToString (context).ToLocalChecked ();
535-
536- res->type = type_str_utf8;
537- res->len = size_t (rstr->Utf8Length (isolate)); // in bytes
538- size_t capacity = res->len + 1 ;
539- res->str_val = xalloc (res->str_val , capacity);
540- rstr->WriteUtf8 (isolate, res->str_val );
556+ } else {
557+ goto err;
541558 }
542559 return res;
543560
@@ -547,6 +564,16 @@ static BinaryValue *convert_v8_to_binary(Isolate * isolate,
547564}
548565
549566
567+ static BinaryValue *convert_basic_v8_to_binary (Isolate * isolate,
568+ const Persistent<Context> & context,
569+ Local<Value> value)
570+ {
571+ HandleScope scope (isolate);
572+ return convert_basic_v8_to_binary (isolate,
573+ Local<Context>::New (isolate, context),
574+ value);
575+ }
576+
550577static BinaryValue *convert_v8_to_binary (Isolate * isolate,
551578 const Persistent<Context> & context,
552579 Local<Value> value)
@@ -610,7 +637,7 @@ ContextInfo *MiniRacer_init_context()
610637static BinaryValue* MiniRacer_eval_context_unsafe (
611638 ContextInfo *context_info,
612639 char *utf_str, int str_len,
613- unsigned long timeout, size_t max_memory)
640+ unsigned long timeout, size_t max_memory, bool basic_only )
614641{
615642 EvalParams eval_params;
616643 EvalResult eval_result{};
@@ -643,6 +670,7 @@ static BinaryValue* MiniRacer_eval_context_unsafe(
643670 eval_params.result = &eval_result;
644671 eval_params.timeout = 0 ;
645672 eval_params.max_memory = 0 ;
673+ eval_params.basic_only = basic_only;
646674 if (timeout > 0 ) {
647675 eval_params.timeout = timeout;
648676 }
@@ -732,7 +760,11 @@ static BinaryValue* MiniRacer_eval_context_unsafe(
732760 HandleScope handle_scope (context_info->isolate );
733761
734762 Local<Value> tmp = Local<Value>::New (context_info->isolate , *eval_result.value );
735- result = convert_v8_to_binary (context_info->isolate , *context_info->context , tmp);
763+ if (eval_params.basic_only ) {
764+ result = convert_basic_v8_to_binary (context_info->isolate , *context_info->context , tmp);
765+ } else {
766+ result = convert_v8_to_binary (context_info->isolate , *context_info->context , tmp);
767+ }
736768 }
737769
738770 BinaryValueFree (bmessage);
@@ -767,8 +799,8 @@ class BufferOutputStream: public OutputStream {
767799
768800extern " C" {
769801
770- LIB_EXPORT BinaryValue * mr_eval_context (ContextInfo *context_info, char *str, int len, unsigned long timeout, size_t max_memory) {
771- BinaryValue *res = MiniRacer_eval_context_unsafe (context_info, str, len, timeout, max_memory);
802+ LIB_EXPORT BinaryValue * mr_eval_context (ContextInfo *context_info, char *str, int len, unsigned long timeout, size_t max_memory, bool basic_only ) {
803+ BinaryValue *res = MiniRacer_eval_context_unsafe (context_info, str, len, timeout, max_memory, basic_only );
772804 return res;
773805}
774806
0 commit comments