I have not found the answer to life, the universe and everything yet...
I created the hello.js based on Floh's code. I did one change in that I
added the "-s WASM=0" to the emcc command line to only produce JS and not
any WASM. (I assume that is ok for my goal.)
I added hello.js to my MS Code project and then try calling the "_addc" in
hello by adding the following in my main TypeScript file app.ts:
at the top of app.ts:
import {_addc} from './app/util/hello.js';
and in the body of app.ts:
const myResult = _addc(10,20);
console.log('....................................' + myResult +
'-------------------------')
======================================
It built, but when I run, I get the following error: native function
`addc` called before runtime initialization
======================================
$ yarn start
yarn run v1.22.10
warning package.json: License should be a valid SPDX license expression
warning ../../package.json: License should be a valid SPDX license
expression
warning ../../../../package.json: No license field
$ node dist/index.js
GPProcessAPIService constructor
Assertion failed: native function `addc` called before runtime
initialization
/Users/me/Desktop/cg-process-api/src/production/cg-process-api/dist/app/util/hello.js:102
throw ex;
^
Error: abort(Assertion failed: native function `addc` called before runtime
initialization) at Error
at jsStackTrace
(/Users/me/Desktop/gp/gp-process-api/gp-process-api/src/production/gp-process-api/dist/app/util/hello.js:1856:17)
at stackTrace
(/Users/me/Desktop/gp/gp-process-api/gp-process-api/src/production/gp-process-api/dist/app/util/hello.js:1873:14)
at abort
(/Users/me/Desktop/gp/gp-process-api/gp-process-api/src/production/gp-process-api/dist/app/util/hello.js:1614:46)
at assert
(/Users/me/Desktop/gp/cg-process-api/gp-process-api/src/production/gp-process-api/dist/app/util/hello.js:856:9)
at Object._addc
(/Users/me/Desktop/gp/gp-process-api/cgps-process-api/src/production/gp-process-api/dist/app/util/hello.js:1671:9)
at Object.<anonymous>
(/Users/me/Desktop/gp/gp-process-api/gp-process-api/src/production/gp-process-api/dist/app.js:59:29)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js
(internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at abort
(/Users/me/Desktop/gp/gp-process-api/gp-process-api/src/production/gp-process-api/dist/app/util/hello.js:1619:13)
at assert
(/Users/me/Desktop/gp/gp-process-api/gp-process-api/src/production/gp-process-api/dist/app/util/hello.js:856:9)
at Object._addc
(/Users/me/Desktop/gp/gp-process-api/gp-process-api/src/production/gp-process-api/dist/app/util/hello.js:1671:9)
at Object.<anonymous>
(/Users/me/Desktop/gp/gp-process-api/gp-process-api/src/production/gp-process-api/dist/app.js:59:29)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js
(internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
error Command failed with exit code 7.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this
command.
$
On Wednesday, April 14, 2021 at 1:21:57 PM UTC-7 HJ wrote:
> Thank you both for the quick responses.
> Floh, I will try this today.
> Geoffrey, I have tried your Generate Javascript Using MATLAB Coder add-on
> in MATLAB, but ran into issues. I posted those issues on the conversation
> that is on that add-on's page.
>
> On Wednesday, April 14, 2021 at 5:13:16 AM UTC-7 [email protected] wrote:
>
>> Hello,
>> You may want to look at:
>>
>> https://www.mathworks.com/matlabcentral/fileexchange/69973-generate-javascript-using-matlab-coder
>> There is an update to be released soon that will resolve the issues shown
>> in the discussion. The existing add-on should work so long as you install
>> emscripten using the following flag:
>>
>> ./emsdk install 1.38.45
>>
>> Hope this helps.
>> On Wednesday, April 14, 2021 at 7:43:02 AM UTC-4 Floh wrote:
>>
>>> Hi, I quickly cobbled together a "simplest possible sample" for calling
>>> a C function from JS here:
>>>
>>> https://github.com/floooh/emsc-interop-demo
>>>
>>> This is running in the browser though and it's a bit quick'n'dirty, but
>>> it should get you started at least
>>> for your own experimentations. The important things are the
>>> EMSCRIPTEN_KEEPALIVE annotation in the
>>> hello.c file, and the underscore in front of the addc() function call in
>>> Javascript in shell.html.
>>>
>>> If you cannot annotate the original source files with
>>> EMSCRIPTEN_KEEPALIVE, you can also provide the
>>> C functions names to make visible to Javascript with a compiler option
>>> instead, see here:
>>>
>>>
>>> https://emscripten.org/docs/getting_started/FAQ.html#why-do-functions-in-my-c-c-source-code-vanish-when-i-compile-to-javascript-and-or-i-get-no-functions-to-process
>>>
>>> I prefer to work with simple C-APIs instead of C++ APIs, because with
>>> C-APIs you
>>> can (more or less) call the functions directly from Javascript instead
>>> of creating a "language binding"
>>> between C++ and Javascript interfaces. For this, emscripten has a tool
>>> called "embind" (which adds a whole
>>> new level of complexity and more things that can break, but I guess
>>> that's personal opinion heh).
>>> There are some caveats with non-trivial function arguments (for instance
>>> pointers to data on the WASM heap),
>>> maybe embind makes this case easier, don't know.
>>>
>>> In any case, here's the embind documentation:
>>>
>>>
>>> https://emscripten.org/docs/porting/connecting_cpp_and_javascript/embind.html
>>>
>>> Hope this helps!
>>>
>>> On Wednesday, 14 April 2021 at 02:43:49 UTC+2 HJ wrote:
>>>
>>>> I am a beginner and I'm sure all the information is there, but I am
>>>> struggling to get even a basic example working. I can run the tutorial
>>>> example, but I can't get anything for the following:
>>>>
>>>> I have converted some MATLAB .m script files to C (MATLAB can convert
>>>> to C or CPP. I've tried both C and CPP in the below information.)
>>>>
>>>> MATLAB has 1 function per 1 file, so now I have two .c files with one c
>>>> function in each.
>>>>
>>>> I've run emcc to convert .c to .js
>>>> When I look in the a.out.js and search for "myfunct", all I see for my
>>>> function is the following and I'm not sure how to call it in my js main
>>>> program:
>>>>
>>>> /** @type {function(...*):?} */ var _myFunc = Module["_myFunc"] =
>>>> createExportWrapper("myFunc");
>>>>
>>>> I've played around with many different command line parameters that
>>>> I've found on various Q&A forums, but still can't figure out how to create
>>>> such that there is a function called "myFunc" that I can call from my
>>>> other
>>>> js files (actually typescript, but just assume js for this.)
>>>>
>>>> This code will be run on the server side, node.js, and not on a
>>>> browser. Backend code that will call these converted MATLAB functions to
>>>> generate data.
>>>>
>>>> It would be nice to have an example from beginning to end (I've been
>>>> searching and searching, but I only find answers that will say things
>>>> like,
>>>> "You just need to use the xxxx option", but there is more to the puzzle
>>>> that I am missing.
>>>>
>>>> It seems like the examples on emscripten site are all self contained:
>>>> you build and run them, but no example of how to convert a function and
>>>> how
>>>> to integrate it (call it) from other js code. I have read the many
>>>> options,
>>>> but again I am not sure exactly what to do. I've also read the comments in
>>>> the produced js file about creating a module and searched, but I am not
>>>> sure how to define a module for a function.
>>>>
>>>> - What are the emcc parameters do I use?
>>>> - Can I convert just this one function or do I need to have a
>>>> main() that calls it?
>>>> - Once converted, it looks like I need to define a module for
>>>> "myfunc", but I am not sure how it is defined, when to define, do I add
>>>> after into the .js?
>>>> - What about using the WASM=0 option to only produce js and no
>>>> WASM. Maybe that is a route to take? (I tried and it produced some
>>>> different code in .js, but i'm still stuck on what to do with it.
>>>>
>>>> I created the below simple function that takes a double and returns a
>>>> double for trying this. If anyone can show me the steps with this or any
>>>> HelloWorld that takes a parameter and returns it would be greatly
>>>> appreciated. (or even more complex example.)
>>>>
>>>> Thank helping this newbie! :-)
>>>>
>>>> *myFunc.c:*
>>>>
>>>> #include "myfunc.h"
>>>> double myfunc(double myInt) {
>>>> return myInt + 50.0;
>>>> }
>>>>
>>>> *myFunc.h:*
>>>>
>>>> #ifndef MYFUNC_H
>>>> #define MYFUNC_H
>>>>
>>>> /* Include Files */
>>>> #include "rtwtypes.h"
>>>> #include <stddef.h>
>>>> #include <stdlib.h>
>>>>
>>>> #ifdef __cplusplus
>>>> extern "C" {
>>>> #endif
>>>>
>>>> /* Function Declarations */
>>>> extern double myFunc(double myInt);
>>>>
>>>> #ifdef __cplusplus
>>>> }
>>>> #endif
>>>>
>>>> #endif
>>>>
>>>
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/emscripten-discuss/1722a215-5822-4afb-957f-c8eb1ab8f6e1n%40googlegroups.com.