Skip to content

Promise inside QuickJs cannot work #67

Closed

Description

@Lyoko-Jeremie

I try to use the libsodium-wrapper in QuickJS follow the document, but it hang when await ready .

after some test, i took the strange thing, the Promise will break the VM then i use the code from document .

the mini code in follow :

import {getQuickJS, QuickJSContext, QuickJSHandle, Scope, shouldInterruptAfterDeadline} from 'quickjs-emscripten';


(async () => {
    const QuickJS = await getQuickJS();

    await Scope.withScopeAsync(async (scope) => {
        const vm = scope.manage(QuickJS.newContext());

        const result = vm.evalCode(
            `
(new Promise((resolve, reject) => {
    setTimeout(args => {
        resolve(undefined);
    }, 1000);
})).then(T=>{});
`
        );

        // run in sync way, it return ok.
        // // output:
        // //    Success: {}
        // //    undefined
        // if (result.error) {
        //     console.log('Execution failed:', vm.dump(result.error));
        //     result.error.dispose();
        // } else {
        //     console.log('Success:', vm.dump(result.value));
        //     result.value.dispose();
        // }
        // return;



        // run in async way, it return into strange ?
        // output:
        //    promiseHandle
        //    resolvedResult
        //    resolvedResultPromise Promise { <pending> }
        //    
        // then the process end.


        console.log('promiseHandle');
        const promiseHandle = (vm.unwrapResult(result));

        console.log('resolvedResult');
        const resolvedResultPromise = vm.resolvePromise(promiseHandle);

        console.log('resolvedResultPromise', resolvedResultPromise);
        const resolvedResult = await resolvedResultPromise;    // <============ end on the `await` op

        console.log('promiseHandle.dispose');    // <============ this code be never run
        promiseHandle.dispose();

        console.log('resolvedHandle');
        const resolvedHandle = (vm.unwrapResult(resolvedResult));

        console.log('resolvedHandle', vm.getString(resolvedHandle));

        console.log('resolvedHandle.dispose');
        resolvedHandle.dispose();
    });
})().then(T => {
    console.log(T);
}).catch(E => {
    console.error(E);
});

anything wrong ?

Activity

  1. justjake commented on Jun 22, 2022

    @justjake
    Owner
  2. Lyoko-Jeremie commented on Jun 22, 2022

    @Lyoko-Jeremie
    Author

    @justjake
    i think so , but the promise create in the vm , use in the vm , and resolve in the vm , where can i call the executePendingJobs ?

  3. Lyoko-Jeremie commented on Jun 22, 2022

    @Lyoko-Jeremie
    Author

    i find a JS_BOOL JS_IsJobPending(JSRuntime *rt); function from quickjs.h

    https://github.com/bellard/quickjs/blob/2788d71e823b522b178db3b3660ce93689534e6d/quickjs.h#L874

    /* JS Job support */
    
    
    typedef JSValue JSJobFunc(JSContext *ctx, int argc, JSValueConst *argv);
    int JS_EnqueueJob(JSContext *ctx, JSJobFunc *job_func, int argc, JSValueConst *argv);
    
    
    JS_BOOL JS_IsJobPending(JSRuntime *rt);
    int JS_ExecutePendingJob(JSRuntime *rt, JSContext **pctx);

    the JS_IsJobPending seems like usefull .


    and after i look up the document, i find some thing :

    https://github.com/justjake/quickjs-emscripten/blob/897fded554b9b1c056c830789f407690ed01a2d9/CHANGELOG.md#breaking-changes

    executependingjobs()
    haspendingjob()

    and from the evalcode() comment :

    If evaluation returned a handle containing a promise, use resolvePromise to convert it to a native promise and executePendingJobs to finish evaluating the promise.

    so, i add vm.runtime.executePendingJobs() call between the vm.resolvePromise and await like follow, the above code work !

            console.log('resolvedResult');
            const resolvedResultPromise = vm.resolvePromise(promiseHandle);
    
    
            // add this
            console.log('vm.runtime.hasPendingJob()', vm.runtime.hasPendingJob());    // <============ return true
            console.log('vm.runtime.executePendingJobs()', vm.unwrapResult(vm.runtime.executePendingJobs()));
            console.log('vm.runtime.hasPendingJob()', vm.runtime.hasPendingJob());    // <============ return false
    
    
            console.log('resolvedResultPromise', resolvedResultPromise);    // <============ pending on this
            const resolvedResult = await resolvedResultPromise;

    seems like the document in Readme.md need some update

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions