How to pass Object key value pair as an argument to JavaScript Function with embind::val?

43 views
Skip to first unread message

gerald-dotcom

unread,
May 19, 2020, 3:06:21 PM5/19/20
to emscripten-discuss

I haven't seen a single example of embind::val that pass Object Key Value Pair as an Argument to JavaScript Functions.

Therefore, I decided to try by structuring JSON-style string with C++ and passing it as an argument to that specific function,

'{' + std::string("brandId") + ':' + std::to_string(brandId) + '}'
However, the Javascript run time not recognize it as an valid Object instead it being parsed as a String literal.

While I'm new to C++, I got slight memory about Struct data type and I decided to give it try, but even though I can pass Struct as an Argument, at run time, it trigger following error,

parameter 1 has unknown type Z9getCarBrandiNSt3__212basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEEiE10brandStruct

gerald-dotcom

unread,
May 19, 2020, 4:07:42 PM5/19/20
to emscripten-discuss

I was able to do following,

    emscripten::val ExampleObject = emscripten::val::object();
    ExampleObjectset("key", value);

It is working, but is this correct way to create native Objects in emscripten?

J Decker

unread,
May 19, 2020, 10:31:22 PM5/19/20
to emscripten-discuss
This is how i did it using EM_ASM_( ) this_.objects is a JS array of objects I can refer to by numeric index from C

https://github.com/d3x0r/jsox-wasm/blob/master/simple_js.c#L273-L279  

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/ef9f8051-a026-4dae-8572-b26794cbc4e7%40googlegroups.com.

gerald-dotcom

unread,
May 20, 2020, 9:34:15 AM5/20/20
to emscripten-discuss
EM_ASM() is inline JS. I'm using emscripten::val to create bindings directly from  pure C++  and looking to verify the solution I posted above is standard or not. 

On Tuesday, May 19, 2020 at 9:31:22 PM UTC-5, J Decker wrote:
This is how i did it using EM_ASM_( ) this_.objects is a JS array of objects I can refer to by numeric index from C

https://github.com/d3x0r/jsox-wasm/blob/master/simple_js.c#L273-L279  

On Tue, May 19, 2020 at 1:07 PM gerald-dotcom <wregr...@gmail.com> wrote:

I was able to do following,

    emscripten::val ExampleObject = emscripten::val::object();
    ExampleObjectset("key", value);

It is working, but is this correct way to create native Objects in emscripten?


On Tuesday, May 19, 2020 at 2:06:21 PM UTC-5, gerald-dotcom wrote:

I haven't seen a single example of embind::val that pass Object Key Value Pair as an Argument to JavaScript Functions.

Therefore, I decided to try by structuring JSON-style string with C++ and passing it as an argument to that specific function,

'{' + std::string("brandId") + ':' + std::to_string(brandId) + '}'
However, the Javascript run time not recognize it as an valid Object instead it being parsed as a String literal.

While I'm new to C++, I got slight memory about Struct data type and I decided to give it try, but even though I can pass Struct as an Argument, at run time, it trigger following error,

parameter 1 has unknown type Z9getCarBrandiNSt3__212basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEEiE10brandStruct

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-discuss+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages