Skip to content

Promise inside QuickJs cannot work #67

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 ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions