From 61c33ebf9906b04409661fd6fc7e70aad0c20ba7 Mon Sep 17 00:00:00 2001 From: Sami Vaarala Date: Sun, 10 Sep 2023 15:58:10 +0300 Subject: [PATCH] Logical assignment operator trivia Avoid C99 comments (//), clearer flag check, releases entry. --- releases/releases.yaml | 1 + src-input/duk_js_compiler.c | 9 +++++---- tests/ecmascript/expr/test-expr-logicalop-assignment.js | 5 +---- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/releases/releases.yaml b/releases/releases.yaml index 45bd2036b..937a5cf9d 100644 --- a/releases/releases.yaml +++ b/releases/releases.yaml @@ -1397,3 +1397,4 @@ duktape_releases: - "Add support for DJGPP (MSDOS) platform (GH-2472, GH-2473)" - "Remove .caller and .arguments own properties from both strict and non-strict function objects, and add .caller and .arguments throwers to Function.prototype to match ES2015+ (GH-2482)" - "Add support for ES2021 digit separator (GH-2537)" + - "Add support for logical assignment operators &&= and ||= (GH-2527)" diff --git a/src-input/duk_js_compiler.c b/src-input/duk_js_compiler.c index f41b5b6b3..8f55ef48c 100644 --- a/src-input/duk_js_compiler.c +++ b/src-input/duk_js_compiler.c @@ -4463,7 +4463,8 @@ DUK_LOCAL void duk__expr_led(duk_compiler_ctx *comp_ctx, duk_ivalue *left, duk_i { duk_regconst_t reg_temp; duk_int_t pc_jump; - duk_small_uint_t args_truthval = args >> 8; + duk_small_uint_t args_truthval = args & 0x100; + duk_small_uint_t args_assignment = args & 0x200; duk_small_uint_t args_rbp = args & 0xff; duk_small_uint_t leftt; @@ -4474,7 +4475,7 @@ DUK_LOCAL void duk__expr_led(duk_compiler_ctx *comp_ctx, duk_ivalue *left, duk_i duk_regconst_t reg_obj; duk_regconst_t rc_key; - if (args_truthval & 2) { // assignment + if (args_assignment) { leftt = left->t; if (leftt == DUK_IVAL_VAR) { DUK_ASSERT(left->x1.t == DUK_ISPEC_VALUE); /* LHS is already side effect free */ @@ -4519,12 +4520,12 @@ DUK_LOCAL void duk__expr_led(duk_compiler_ctx *comp_ctx, duk_ivalue *left, duk_i duk__ivalue_toforcedreg(comp_ctx, left, reg_temp); DUK_ASSERT(DUK__ISREG(reg_temp)); duk__emit_bc(comp_ctx, - ((args_truthval & 1) ? DUK_OP_IFTRUE_R : DUK_OP_IFFALSE_R), + (args_truthval ? DUK_OP_IFTRUE_R : DUK_OP_IFFALSE_R), reg_temp); /* skip jump conditionally */ pc_jump = duk__emit_jump_empty(comp_ctx); duk__expr_toforcedreg(comp_ctx, res, args_rbp /*rbp_flags*/, reg_temp /*forced_reg*/); - if (args_truthval & 2) { // assignment + if (args_assignment) { if (leftt == DUK_IVAL_VAR) { if (reg_varbind >= 0) { duk__emit_a_bc(comp_ctx, DUK_OP_LDREG, reg_varbind, reg_temp); diff --git a/tests/ecmascript/expr/test-expr-logicalop-assignment.js b/tests/ecmascript/expr/test-expr-logicalop-assignment.js index 00594138b..155e9315f 100644 --- a/tests/ecmascript/expr/test-expr-logicalop-assignment.js +++ b/tests/ecmascript/expr/test-expr-logicalop-assignment.js @@ -35,8 +35,6 @@ var b = a ||= testSideEffect(0, '3') || testSideEffect(0, '4') || testSideEffect print(a); // Should be false, not 0 print(b); // Should be false, not 0 - - /*=== [object Object] false @@ -54,7 +52,6 @@ true testSideEffect(obj, '6').b ||= true; print(obj.b); - /*=== tse 7 tse 8 @@ -64,4 +61,4 @@ tse 10 ===*/ testSideEffect(obj, '7').b &&= testSideEffect(obj, '8').b &&= testSideEffect(obj, '9').b = testSideEffect(99, '10'); -print(obj.b); \ No newline at end of file +print(obj.b);