Skip to content

Commit 0a79b84

Browse files
committed
Improve deserializer error message for bytecode
Don't raise a "invalid tag 12" exception when encountering bytecode and JS_READ_OBJ_BYTECODE is not set, because no one knows what "tag 12" means without looking it up, not even quickjs maintainers.
1 parent 4fbce79 commit 0a79b84

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

quickjs-libc.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,7 @@ static JSValue js_evalScript(JSContext *ctx, JSValue this_val,
837837
JSValue ret;
838838
JSValue options_obj;
839839
BOOL backtrace_barrier = FALSE;
840+
BOOL compile_only = FALSE;
840841
BOOL is_async = FALSE;
841842
int flags;
842843

@@ -845,6 +846,9 @@ static JSValue js_evalScript(JSContext *ctx, JSValue this_val,
845846
if (get_bool_option(ctx, &backtrace_barrier, options_obj,
846847
"backtrace_barrier"))
847848
return JS_EXCEPTION;
849+
if (get_bool_option(ctx, &compile_only, options_obj,
850+
"compile_only"))
851+
return JS_EXCEPTION;
848852
if (get_bool_option(ctx, &is_async, options_obj,
849853
"async"))
850854
return JS_EXCEPTION;
@@ -860,6 +864,8 @@ static JSValue js_evalScript(JSContext *ctx, JSValue this_val,
860864
flags = JS_EVAL_TYPE_GLOBAL;
861865
if (backtrace_barrier)
862866
flags |= JS_EVAL_FLAG_BACKTRACE_BARRIER;
867+
if (compile_only)
868+
flags |= JS_EVAL_FLAG_COMPILE_ONLY;
863869
if (is_async)
864870
flags |= JS_EVAL_FLAG_ASYNC;
865871
ret = JS_Eval(ctx, str, len, "<evalScript>", flags);

quickjs.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35473,12 +35473,14 @@ static JSValue JS_ReadObjectRec(BCReaderState *s)
3547335473
break;
3547435474
case BC_TAG_FUNCTION_BYTECODE:
3547535475
if (!s->allow_bytecode)
35476-
goto invalid_tag;
35476+
goto no_allow_bytecode;
3547735477
obj = JS_ReadFunctionTag(s);
3547835478
break;
3547935479
case BC_TAG_MODULE:
35480-
if (!s->allow_bytecode)
35481-
goto invalid_tag;
35480+
if (!s->allow_bytecode) {
35481+
no_allow_bytecode:
35482+
return JS_ThrowSyntaxError(ctx, "no bytecode allowed");
35483+
}
3548235484
obj = JS_ReadModule(s);
3548335485
break;
3548435486
case BC_TAG_OBJECT:

tests/test_bjson.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as std from "std";
12
import * as bjson from "bjson";
23
import { assert } from "./assert.js";
34

@@ -227,6 +228,25 @@ function bjson_test_symbol()
227228
assert(o, r);
228229
}
229230

231+
function bjson_test_bytecode()
232+
{
233+
var buf, o, r, e;
234+
235+
o = std.evalScript(";(function f(o){ return o.i })", {compile_only: true});
236+
buf = bjson.write(o, /*JS_WRITE_OBJ_BYTECODE*/(1 << 0));
237+
try {
238+
bjson.read(buf, 0, buf.byteLength);
239+
} catch (_e) {
240+
e = _e;
241+
}
242+
assert(String(e), "SyntaxError: no bytecode allowed");
243+
244+
// can't really do anything with |o| at the moment,
245+
// no way to pass it to JS_EvalFunction
246+
o = bjson.read(buf, 0, buf.byteLength, /*JS_READ_OBJ_BYTECODE*/(1 << 0));
247+
assert(String(o), "[function bytecode]");
248+
}
249+
230250
function bjson_test_fuzz()
231251
{
232252
var corpus = [
@@ -277,6 +297,7 @@ function bjson_test_all()
277297
bjson_test_map();
278298
bjson_test_set();
279299
bjson_test_symbol();
300+
bjson_test_bytecode();
280301
bjson_test_fuzz();
281302
}
282303

0 commit comments

Comments
 (0)