daminetreg / js-bind

js::bind makes any C++ function, member function or lambda as a Javascript callback.
Boost Software License 1.0
22 stars 9 forks source link

Error When bind function with 0 args. #1

Open Satancito opened 4 years ago

Satancito commented 4 years ago

Hello.

I convert LoadScript function from Js to C++, set events onload and onerror all ok , but I get following error when onload event is launched.

Summary: Script is loaded but when onload is executed get error.

image

image

C++ Function

emscripten::val LoadScript(emscripten::val scriptpath)
{
    using namespace emscripten;
    String hash = scriptpath.as<String>();
    if(!val::global("VMain")["Loaded"][hash].isUndefined())
    {
        auto promiseCallback = [&](val resolve, val reject){
            resolve(hash);
        };
        return val::global("Promise").new_(js::bind(promiseCallback, std::placeholders::_1, std::placeholders::_2));
    }
    auto promiseCallback = [&](val resolve, val reject) -> void{
            resolve();
            val script = val::global("document").call<val>("createElement", val("script"));
            auto onloadCallback = [&]() -> void{ //NOT EXECUTED - check errors
                emscripten_console_log("On load");
                resolve(hash);
            };
            script.set("onload", js::bind(onloadCallback)); //No errors here 

            auto onerrorCallback = [&]() -> void{ //NOT EXECUTED - check errors
                emscripten_console_log("On error");
                reject(hash);
            };
            script.set("onerror", js::bind(onerrorCallback)); //No errors here

            script.set("src",scriptpath);
            script.set("type", val("text/javascript"));
            val::global("VMain")["Loaded"].set(hash, val(true));
            val::global("document")["body"].call<val>("appendChild", script);
    };
    return val::global("window")["Promise"].new_(js::bind(promiseCallback, std::placeholders::_1, std::placeholders::_2));
}

Original JS Code.

window.VMain = {};
window.VMain.Loaded = [];

window.VMain.LoadScript = function (scriptPath) {
    if (window.VMain.Loaded[scriptPath]) {
        return new this.Promise(function (resolve, reject) {
            resolve();
        });
    }
    return new Promise(function (resolve, reject) {
        var script = document.createElement("script");
        script.src = scriptPath;
        script.type = "text/javascript";
        window.VMain.Loaded[scriptPath] = true;
        script.onload = function () {
            resolve(scriptPath);
        };
        script.onerror = function () {
            reject(scriptPath);
        };
        document["body"].appendChild(script);
    });
};

Thanks for your help.


EXTRAS

Full error text

VModule.js:9 Uncaught BindingError {name: "BindingError", message: "function void0ArgsFunctor.opcall called with 1 arguments, expected 0 args!", stack: "BindingError: function void0ArgsFunctor.opcall cal…alhost:8082/VModule.js:9:95572), <anonymous>:5:1)"}
throwBindingError @ VModule.js:9
void0ArgsFunctor$opcall @ VM1906:5
load (asincrónico)
__emval_set_property @ VModule.js:9
(anónimo) @ VModule.wasm:1
ret.<computed> @ VModule.js:9
invoke_viii @ VModule.js:9
(anónimo) @ VModule.wasm:1
(anónimo) @ VModule.wasm:1
(anónimo) @ VModule.wasm:1
ret.<computed> @ VModule.js:9
invoke_viii @ VModule.js:9
(anónimo) @ VModule.wasm:1
(anónimo) @ VModule.wasm:1
ret.<computed> @ VModule.js:9
Module.dynCall_viiii @ VModule.js:9
dynCall_viiii_36 @ VM1759:4
void2ArgsFunctor$opcall @ VM1916:10
emval_allocator_1 @ VM1979:7
__emval_new @ VModule.js:9
(anónimo) @ VModule.wasm:1
ret.<computed> @ VModule.js:9
invoke_iiiii @ VModule.js:9
(anónimo) @ VModule.wasm:1
(anónimo) @ VModule.wasm:1
ret.<computed> @ VModule.js:9
invoke_vii @ VModule.js:9
(anónimo) @ VModule.wasm:1
(anónimo) @ VModule.wasm:1
(anónimo) @ VModule.wasm:1
ret.<computed> @ VModule.js:9
dynCall_iii_17 @ VM1744:4
Property @ VM1912:8
eval @ VM1976:3
_emscripten_run_script @ VModule.js:9
(anónimo) @ VModule.wasm:1
ret.<computed> @ VModule.js:9
Module._main @ VModule.js:9
callMain @ VModule.js:9
doRun @ VModule.js:9
run @ VModule.js:9
runCaller @ VModule.js:9
removeRunDependency @ VModule.js:9
receiveInstance @ VModule.js:9
receiveInstantiatedSource @ VModule.js:9
Promise.then (asincrónico)
(anónimo) @ VModule.js:9
Promise.then (asincrónico)
instantiateAsync @ VModule.js:9
createWasm @ VModule.js:9
(anónimo) @ VModule.js:9
(anónimo) @ Instance.js:1
Mostrar 14 fotogramas más del código de biblioteca
VModule.js:9 Uncaught BindingError {name: "BindingError", message: "function void0ArgsFunctor.opcall called with 1 arguments, expected 0 args!", stack: "BindingError: function void0ArgsFunctor.opcall cal…alhost:8082/VModule.js:9:95572), <anonymous>:5:1)"}message: "function void0ArgsFunctor.opcall called with 1 arguments, expected 0 args!"name: "BindingError"stack: "BindingError: function void0ArgsFunctor.opcall called with 1 arguments, expected 0 args!↵    at BindingError.<anonymous> (http://localhost:8082/VModule.js:9:75971)↵    at new BindingError (eval at createNamedFunction (http://localhost:8082/VModule.js:9:75680), <anonymous>:4:34)↵    at throwBindingError (http://localhost:8082/VModule.js:9:76412)↵    at void0ArgsFunctor$opcall [as opcall] (eval at new_ (http://localhost:8082/VModule.js:9:95572), <anonymous>:5:1)"__proto__: Errorconstructor: ƒ BindingError()arguments: (...)caller: (...)length: 0name: "BindingError"prototype: Error {constructor: ƒ, toString: ƒ}__proto__: ƒ ()[[FunctionLocation]]: VM1735:3[[Scopes]]: Scopes[2]toString: ƒ ()arguments: nullcaller: nulllength: 0name: ""prototype: {constructor: ƒ}__proto__: ƒ ()[[FunctionLocation]]: VModule.js:9[[Scopes]]: Scopes[4]__proto__: constructor: ƒ Error()message: ""name: "Error"toString: ƒ toString()arguments: (...)caller: (...)length: 0name: "toString"__proto__: ƒ ()[[Scopes]]: Scopes[0]__proto__: Object
throwBindingError @ VModule.js:9
void0ArgsFunctor$opcall @ VM1906:5
load (asincrónico)
__emval_set_property @ VModule.js:9
(anónimo) @ VModule.wasm:1
ret.<computed> @ VModule.js:9
invoke_viii @ VModule.js:9
(anónimo) @ VModule.wasm:1
(anónimo) @ VModule.wasm:1
(anónimo) @ VModule.wasm:1
ret.<computed> @ VModule.js:9
invoke_viii @ VModule.js:9
(anónimo) @ VModule.wasm:1
(anónimo) @ VModule.wasm:1
ret.<computed> @ VModule.js:9
Module.dynCall_viiii @ VModule.js:9
dynCall_viiii_36 @ VM1759:4
void2ArgsFunctor$opcall @ VM1916:10
emval_allocator_1 @ VM1979:7
__emval_new @ VModule.js:9
(anónimo) @ VModule.wasm:1
ret.<computed> @ VModule.js:9
invoke_iiiii @ VModule.js:9
(anónimo) @ VModule.wasm:1
(anónimo) @ VModule.wasm:1
ret.<computed> @ VModule.js:9
invoke_vii @ VModule.js:9
(anónimo) @ VModule.wasm:1
(anónimo) @ VModule.wasm:1
(anónimo) @ VModule.wasm:1
ret.<computed> @ VModule.js:9
dynCall_iii_17 @ VM1744:4
Property @ VM1912:8
eval @ VM1976:8
Promise.then (asincrónico)
eval @ VM1976:7
_emscripten_run_script @ VModule.js:9
(anónimo) @ VModule.wasm:1
ret.<computed> @ VModule.js:9
Module._main @ VModule.js:9
callMain @ VModule.js:9
doRun @ VModule.js:9
run @ VModule.js:9
runCaller @ VModule.js:9
removeRunDependency @ VModule.js:9
receiveInstance @ VModule.js:9
receiveInstantiatedSource @ VModule.js:9
Promise.then (asincrónico)
(anónimo) @ VModule.js:9
Promise.then (asincrónico)
instantiateAsync @ VModule.js:9
createWasm @ VModule.js:9
(anónimo) @ VModule.js:9
(anónimo) @ Instance.js:1
VModule.js:9 Uncaught BindingError {name: "BindingError", message: "function void0ArgsFunctor.opcall called with 1 arguments, expected 0 args!", stack: "BindingError: function void0ArgsFunctor.opcall cal…alhost:8082/VModule.js:9:95572), <anonymous>:5:1)"}