quickjs icon indicating copy to clipboard operation
quickjs copied to clipboard

Cannot parse simple js code...

Open Honya2000 opened this issue 1 year ago • 4 comments

Hello,

Today I first tried this JS runtime, but unfortunately cannot parse simple js code:

Code looks like this:

var s_Version = function()
{
    const verSplit = Array(3);
    let vv = 125;
    for (let n = 0; n < 3; n++) {
      verSplit[n] = vv & 65535;
      vv = Math.round((vv - verSplit[n]) / 65536);
    }
    const [v0,v1,v2] = verSplit;

    return EngVersion(v0, v1, v2);
}

class EngVersion
{
  constructor(v0, v1, 2)
  {
    this.v0 = v0;
    this.v1 = v1;
    this.v2 = v2;
  }
}

it throws runtime error here:

               put_lvalue(s, opcode, scope, var_name,
                          label_lvalue, PUT_LVALUE_NOKEEP_DEPTH,
                          (tok == TOK_CONST || tok == TOK_LET));

line 24406 of quickjs.c in function js_parse_destructuring_element. Because label_lvalue is uninitialized.

during parsing of this js line:

    const [v0,v1,v2] = verSplit;

Is this construction is not supported by runtime ?

Honya2000 avatar Jun 25 '24 17:06 Honya2000

Hmm, no any comments ?

I made workaround to this code to make it parseable: instead of

        const [v0,v1,v2] = verSplit;

i made this:

        const v0 = verSplit[0]; 
        const v1 = verSplit[1]; 
        const v2 = verSplit[2];  

But problem is I have lot of such constructions in the code:

for example this code also cannot be parsed:

                for (const [i,child] of original.children.entries()) {
                    updateMappings(child, clone.children[i])
                }

Honya2000 avatar Jun 26 '24 07:06 Honya2000

In many situations i even don't know how to make proper workaround: Here for example it iterates through array of objects:

        for (const [key,val] of Object.entries(inputMap)) {

Don't think this code will work the same way:

        for (const i = 0; i < inputMap.length; i++) {
            const key = inputMap[i][0];
            const val = inputMap[i][1];

Honya2000 avatar Jun 26 '24 08:06 Honya2000

It looks like destructuring isn't supported in quickjs. The workaround is to avoid it, so in case of you not being sure how, I would recommend use a babel repl (which supports transformations from ES* versions to ES5/ES3 versions of the language). There's a bit of boilerplate, but you might get the idea: https://babeljs.io/repl#?browsers=defaults%2C%20not%20ie%2011%2C%20not%20ie_mob%2011&build=&builtIns=false&corejs=false&spec=true&loose=false&code_lz=GYewTgBAFAxiB2BnALhA2gawKYE8A0AbgIYA2AuhCMBAPIBGAVljMgHRbzJgCWWiU3eAAcArsgCyRIQEppEAN4AoAL5A&debug=false&forceAllTransforms=true&modules=false&shippedProposals=true&circleciRepo=&evaluate=false&fileSize=false&timeTravel=false&sourceType=script&lineWrap=false&presets=env&prettier=false&targets=&version=7.24.7&externalPlugins=babel-plugin-syntax-async-functions%406.13.0%2C%40babel%2Fplugin-syntax-top-level-await%407.14.5%2C%40babel%2Fplugin-syntax-async-generators%407.8.4%2C%40babel%2Fplugin-transform-runtime%407.23.2%2Cbabel-plugin-transform-async-to-promises%400.8.18&assumptions=%7B%7D

In fact, if you want to keep your JavaScript untouched you could probably use babel (or any alternative such as https://swc.rs) to make sure quickjs works properly.

davesnx avatar Jun 26 '24 10:06 davesnx

@davesnx destructuring is supported in quickjs, but there seems to be a bug in the specific case posted by @Honya2000 . I shall take a look shortly. Sorry for the lag.

chqrlie avatar Jun 26 '24 11:06 chqrlie

I think was was fixed on master?:

var s_Version = function()
{
    const verSplit = Array(3);
    let vv = 125;
    for (let n = 0; n < 3; n++) {
      verSplit[n] = vv & 65535;
      vv = Math.round((vv - verSplit[n]) / 65536);
    }
    const [v0,v1,v2] = verSplit;

    return new EngVersion(v0, v1, v2);
}

class EngVersion
{
  constructor(v0, v1, v2)
  {
    this.v0 = v0;
    this.v1 = v1;
    this.v2 = v2;
  }
}

o = s_Version()
print(o.v0, o.v1, o.v2)

(The original script had a few errors, forgetting a new and using 2 instead of v2)

% ./qjs t.js
125 0 0

nickva avatar Apr 21 '25 20:04 nickva