Re: WASM reverse engineering protection

2021-04-14 Thread Shlomi Fish
Hi Mehaboob!

On Wed, 14 Apr 2021 17:21:49 -0700
J Decker  wrote:

> If code can be run, it can be reverse engineered; just a matter of how much
> work one wants to put into it.
> If the code was compressed, and encrypted, then there'd still be a small
> bit code that has the decrypt, which can be extracted to get the rest, etc.
> Remote checking is a better option; requiring a bit of external code that
> encodes the license in a varying way, for submission to the server, which
> might reply with some small code which enables it to actually function.
> Of course - the really diligent would still just capture the reply and
> bundle it all up statically.
> 
> It's just a matter of how much time you want to make them spend... but much
> like a lock that can be opened can be picked, code that runs can be 'run'
> virtually, using just pen and paper.
> 

In addition, refer to:

https://github.com/shlomif/Freenode-programming-channel-FAQ/blob/master/FAQ_with_ToC__generated.md#how-do-i-hideobscureencrypt-my-source-code-to-prevent-end-users-from-learning-how-it-works

(short URL: https://is.gd/SLSseX .)

It is a decades' old question that keeps getting asked.

Regards,

Shlomi

> On Wed, Apr 14, 2021 at 12:43 PM Mehaboob kk  wrote:
> 
> > Hello All,
> >
> > Is there any technique to protect the WASM code from reverse engineering?
> > I have a licensed software which need to be protected. I am worried that
> > the reverse engineered code can be modified to bypass the license and
> > compile it back again
> >
> > Any inputs please
> >
> > Thanks,
> > Mehaboob
> >
> > --
> > 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+unsubscr...@googlegroups.com.
> > To view this discussion on the web visit
> > https://groups.google.com/d/msgid/emscripten-discuss/d9c09212-cad8-40e1-ba4c-88979f8437a2n%40googlegroups.com
> > 
> > .
> >  
> 



-- 

Shlomi Fish   https://www.shlomifish.org/
Funny Anti-Terrorism Story - https://shlom.in/enemy

Chuck Norris wrote solutions for all the problems of
https://en.wikipedia.org/wiki/Project_Euler , and they all run in under a
minute. In total.
— https://www.shlomifish.org/humour/bits/facts/Chuck-Norris/

Please reply to list if it's a mailing list post - https://shlom.in/reply .

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/emscripten-discuss/20210415082208.2b18631c%40telaviv.shlomifish.org.


Re: Beginner Needs Help on converting and calling a C function

2021-04-14 Thread HJ
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. 
(/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. 
(/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 gmcv...@gmail.com 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 t

Re: WASM reverse engineering protection

2021-04-14 Thread J Decker
If code can be run, it can be reverse engineered; just a matter of how much
work one wants to put into it.
If the code was compressed, and encrypted, then there'd still be a small
bit code that has the decrypt, which can be extracted to get the rest, etc.
Remote checking is a better option; requiring a bit of external code that
encodes the license in a varying way, for submission to the server, which
might reply with some small code which enables it to actually function.
Of course - the really diligent would still just capture the reply and
bundle it all up statically.

It's just a matter of how much time you want to make them spend... but much
like a lock that can be opened can be picked, code that runs can be 'run'
virtually, using just pen and paper.

On Wed, Apr 14, 2021 at 12:43 PM Mehaboob kk  wrote:

> Hello All,
>
> Is there any technique to protect the WASM code from reverse engineering?
> I have a licensed software which need to be protected. I am worried that
> the reverse engineered code can be modified to bypass the license and
> compile it back again
>
> Any inputs please
>
> Thanks,
> Mehaboob
>
> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/emscripten-discuss/d9c09212-cad8-40e1-ba4c-88979f8437a2n%40googlegroups.com
> 
> .
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/emscripten-discuss/CAA2GJqWtWBgix7K3VYT7NiUmRW4R%3DMHKsy570GiX_K7uhCbovA%40mail.gmail.com.


Re: Beginner Needs Help on converting and calling a C function

2021-04-14 Thread HJ
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 gmcv...@gmail.com 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  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 sim

Re: Using custom libs .a files

2021-04-14 Thread 'Thomas Lively' via emscripten-discuss
Hi Mehaboob,

No, Emscripten cannot compile native objects or archive files to
WebAssembly. Perhaps what you saw was a reference to archive files
containing WebAssembly objects produced by Emscripten?

Thomas

On Wed, Apr 14, 2021 at 12:45 Mehaboob kk  wrote:

> Hello All,
>
> I see that Emscripton allows to compile .a files to wasm. In order for
> this to work the .a file need to be originally compiled using gcc? if yes,
> any specific version to be compatible?
>
> Thanks,
> Mehaboob
>
> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/emscripten-discuss/ee46348f-db22-406d-b85d-7dae3aa57e8an%40googlegroups.com
> 
> .
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/emscripten-discuss/CAJZD_EUz2Mkh3S1%2Bh7iGox2Pk3KZWOvMKOrr8a7sNSoZyG7rnw%40mail.gmail.com.


Using custom libs .a files

2021-04-14 Thread Mehaboob kk
Hello All,

I see that Emscripton allows to compile .a files to wasm. In order for this 
to work the .a file need to be originally compiled using gcc? if yes, any 
specific version to be compatible? 

Thanks,
Mehaboob

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/emscripten-discuss/ee46348f-db22-406d-b85d-7dae3aa57e8an%40googlegroups.com.


WASM reverse engineering protection

2021-04-14 Thread Mehaboob kk
Hello All,

Is there any technique to protect the WASM code from reverse engineering? I 
have a licensed software which need to be protected. I am worried that the 
reverse engineered code can be modified to bypass the license and compile 
it back again

Any inputs please

Thanks,
Mehaboob

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/emscripten-discuss/d9c09212-cad8-40e1-ba4c-88979f8437a2n%40googlegroups.com.


Re: Using python literals for EM_CONFIG/--em-config

2021-04-14 Thread Floh
I'm not on Discord, just mention me in the ticket (floooh) and I'll be 
there ;)

On Wednesday, 14 April 2021 at 18:45:57 UTC+2 s...@google.com wrote:

> On Wed, Apr 14, 2021 at 2:44 AM Floh  wrote:
>
>> > Is there some reason you don't want to add the emscripten directory to 
>> you PATH?  Is that somehow worse than setting EM_CONFIG and then parsing it?
>>
>> Because there isn't a single "emscripten directory", but one emscripten 
>> directory per "workspace directory". I might cd to a different workspace 
>> with its own embedded emscripten SDK installation at any time in the same 
>> terminal session. With my current setup, the correct emscripten SDK is 
>> automatically selected depending on the directory the user is currently in 
>> (and then when running cmake to generate the build scripts, cmake will 
>> "bake" the absolute paths into the generated build scripts based on a cmake 
>> option which points to EMSCRIPTEN_ROOT (which however doesn't exist as 
>> environment variable)).
>>
>> > You say you are setting `EM_CONFIG` in your environment [...]  Is there 
>> some reason you couldn't alternatively set `EMSCRIPTEN_ROOT`?
>>
>> EM_CONFIG is not set persistently in the user environment, it's only set 
>> temporary inside a python script (as "os.environ['EM_CONFIG'] = ...") 
>> before the .emscripten file is evaluated, and only because of this one line 
>> in the .emscripten file: 
>>
>> emsdk_path = os.path.dirname(os.environ.get('EM_CONFIG')).replace('\\', 
>> '/')
>>
>> The EM_CONFIG environment variable doesn't survive execution of this 
>> python script however.
>>
>> > to point to emscripten directly via `EMSCRIPTEN_ROOT` ...
>>
>> Isn't this a chicken-egg problem? The EMSCRIPTEN_ROOT variable isn't set 
>> anywhere on my machine, except after running the platform-specific 
>> emsdk_env.xxx scripts (which I don't usually do, except when I need emcc in 
>> the path for some reason). I'd need to run this script (or 'emsdk 
>> construct_env') of a specific emscripten SDK installation from inside 
>> python. But running emsdk from within Python which sets environment 
>> variables doesn't pass this new environment up to the Python process where 
>> the environment variables would be needed for invoking cmake (AFAIK 
>> environment variables can only be inherited from parent to child processes, 
>> but not the other way around).
>>
>> All in all, the way the current Emscripten SDK works when activated as 
>> "--embedded" is pretty much perfect because everything already works 
>> without anything in the PATH or in environment variables. The only wonky 
>> part is how to get from the SDK installation directory (where 'emsdk' is 
>> located - which is under my control) to the 'compiler toolchain directory' 
>> which is needed by cmake (e.g. "[emsdk_dir]/upstream/emscripten"). I 
>> consider the current way to evaluate .emscripten inside python a hack, but 
>> it's a reasonably simple hack that works ;)
>>
>> Any alternative should be simpler then that though ("emsdk construct_env" 
>> does a lot more things then just setting up 'EMSCRIPTEN_ROOT', and as I 
>> described above, doesn't really work for my use case because the modified 
>> environment is "lost" in a child process).
>>
>>
> Thanks for all the details.  I feel confident we can come up with a nicer 
> version of your current hack if we work together.   I will open an emsdk 
> issue for this and maybe reach out to you on discord (are you there?) to 
> chat about the details.
>
> cheers,
> sam
>
> Phew :)
>> -Floh.
>>
>> On Wednesday, 14 April 2021 at 08:16:57 UTC+2 s...@google.com wrote:
>>
>>> On Tue, Apr 13, 2021 at 1:27 PM Floh  wrote:
>>>
 Yeah, I never used python literals in EM_CONFIG, misunderstanding on my 
 part :)

 About the other thing:

 > a) Use `which emcc` to find where emscripten is

 In my case, emcc is not in the path because this is a local 
 installation (via "emsdk activate --embedded") without any traces in the 
 environment (I'm setting EM_CONFIG only temporary in the "python 
 environment" before evaluating .emscripten as a workaround.


>>> Is there some reason you don't want to add the emscripten directory to 
>>> you PATH?  Is that somehow worse than setting EM_CONFIG and then parsing it?
>>>
>>> > b) Have emsdk set `EMSCRIPTEN_ROOT` directly in the environment rather 
 than in the config file. 

 ...this also isn't an option for the same reason as above, there is no 
 "global emscripten installation" at all, and there may be multiple 
 installations of the emscripten SDK with different versions side by side.


>>> I'm not sure I understand.  You say you are setting `EM_CONFIG` in your 
>>> environment according to your needs, so that there is not one global 
>>> `EM_CONFIG`.  Is there some reason you couldn't alternatively set 
>>> `EMSCRIPTEN_ROOT`?
>>>
>>> I don't see how either of these imply some "global emscripten 
>>> installati

Re: Using python literals for EM_CONFIG/--em-config

2021-04-14 Thread 'Sam Clegg' via emscripten-discuss
On Wed, Apr 14, 2021 at 2:44 AM Floh  wrote:

> > Is there some reason you don't want to add the emscripten directory to
> you PATH?  Is that somehow worse than setting EM_CONFIG and then parsing it?
>
> Because there isn't a single "emscripten directory", but one emscripten
> directory per "workspace directory". I might cd to a different workspace
> with its own embedded emscripten SDK installation at any time in the same
> terminal session. With my current setup, the correct emscripten SDK is
> automatically selected depending on the directory the user is currently in
> (and then when running cmake to generate the build scripts, cmake will
> "bake" the absolute paths into the generated build scripts based on a cmake
> option which points to EMSCRIPTEN_ROOT (which however doesn't exist as
> environment variable)).
>
> > You say you are setting `EM_CONFIG` in your environment [...]  Is there
> some reason you couldn't alternatively set `EMSCRIPTEN_ROOT`?
>
> EM_CONFIG is not set persistently in the user environment, it's only set
> temporary inside a python script (as "os.environ['EM_CONFIG'] = ...")
> before the .emscripten file is evaluated, and only because of this one line
> in the .emscripten file:
>
> emsdk_path = os.path.dirname(os.environ.get('EM_CONFIG')).replace('\\',
> '/')
>
> The EM_CONFIG environment variable doesn't survive execution of this
> python script however.
>
> > to point to emscripten directly via `EMSCRIPTEN_ROOT` ...
>
> Isn't this a chicken-egg problem? The EMSCRIPTEN_ROOT variable isn't set
> anywhere on my machine, except after running the platform-specific
> emsdk_env.xxx scripts (which I don't usually do, except when I need emcc in
> the path for some reason). I'd need to run this script (or 'emsdk
> construct_env') of a specific emscripten SDK installation from inside
> python. But running emsdk from within Python which sets environment
> variables doesn't pass this new environment up to the Python process where
> the environment variables would be needed for invoking cmake (AFAIK
> environment variables can only be inherited from parent to child processes,
> but not the other way around).
>
> All in all, the way the current Emscripten SDK works when activated as
> "--embedded" is pretty much perfect because everything already works
> without anything in the PATH or in environment variables. The only wonky
> part is how to get from the SDK installation directory (where 'emsdk' is
> located - which is under my control) to the 'compiler toolchain directory'
> which is needed by cmake (e.g. "[emsdk_dir]/upstream/emscripten"). I
> consider the current way to evaluate .emscripten inside python a hack, but
> it's a reasonably simple hack that works ;)
>
> Any alternative should be simpler then that though ("emsdk construct_env"
> does a lot more things then just setting up 'EMSCRIPTEN_ROOT', and as I
> described above, doesn't really work for my use case because the modified
> environment is "lost" in a child process).
>
>
Thanks for all the details.  I feel confident we can come up with a nicer
version of your current hack if we work together.   I will open an emsdk
issue for this and maybe reach out to you on discord (are you there?) to
chat about the details.

cheers,
sam

Phew :)
> -Floh.
>
> On Wednesday, 14 April 2021 at 08:16:57 UTC+2 s...@google.com wrote:
>
>> On Tue, Apr 13, 2021 at 1:27 PM Floh  wrote:
>>
>>> Yeah, I never used python literals in EM_CONFIG, misunderstanding on my
>>> part :)
>>>
>>> About the other thing:
>>>
>>> > a) Use `which emcc` to find where emscripten is
>>>
>>> In my case, emcc is not in the path because this is a local installation
>>> (via "emsdk activate --embedded") without any traces in the environment
>>> (I'm setting EM_CONFIG only temporary in the "python environment" before
>>> evaluating .emscripten as a workaround.
>>>
>>>
>> Is there some reason you don't want to add the emscripten directory to
>> you PATH?  Is that somehow worse than setting EM_CONFIG and then parsing it?
>>
>> > b) Have emsdk set `EMSCRIPTEN_ROOT` directly in the environment rather
>>> than in the config file.
>>>
>>> ...this also isn't an option for the same reason as above, there is no
>>> "global emscripten installation" at all, and there may be multiple
>>> installations of the emscripten SDK with different versions side by side.
>>>
>>>
>> I'm not sure I understand.  You say you are setting `EM_CONFIG` in your
>> environment according to your needs, so that there is not one global
>> `EM_CONFIG`.  Is there some reason you couldn't alternatively set
>> `EMSCRIPTEN_ROOT`?
>>
>> I don't see how either of these imply some "global emscripten
>> installation".  In both cases the current environment determins the
>> installation to use, no?.   In other words, would it be possible (in your
>> setup) to point to emscripten directly via `EMSCRIPTEN_ROOT` rather than
>> pointing at a config file via `EM_CONFIG` (and then parsing the config file
>> to find EMSCRIPTEN_RO

Re: Beginner Needs Help on converting and calling a C function

2021-04-14 Thread Geoffrey McVittie
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  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

Re: Beginner Needs Help on converting and calling a C function

2021-04-14 Thread Floh
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  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 
> #include 
>
> #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 emscripten-discuss+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msg

Re: Using python literals for EM_CONFIG/--em-config

2021-04-14 Thread Floh
> Is there some reason you don't want to add the emscripten directory to 
you PATH?  Is that somehow worse than setting EM_CONFIG and then parsing it?

Because there isn't a single "emscripten directory", but one emscripten 
directory per "workspace directory". I might cd to a different workspace 
with its own embedded emscripten SDK installation at any time in the same 
terminal session. With my current setup, the correct emscripten SDK is 
automatically selected depending on the directory the user is currently in 
(and then when running cmake to generate the build scripts, cmake will 
"bake" the absolute paths into the generated build scripts based on a cmake 
option which points to EMSCRIPTEN_ROOT (which however doesn't exist as 
environment variable)).

> You say you are setting `EM_CONFIG` in your environment [...]  Is there 
some reason you couldn't alternatively set `EMSCRIPTEN_ROOT`?

EM_CONFIG is not set persistently in the user environment, it's only set 
temporary inside a python script (as "os.environ['EM_CONFIG'] = ...") 
before the .emscripten file is evaluated, and only because of this one line 
in the .emscripten file: 

emsdk_path = os.path.dirname(os.environ.get('EM_CONFIG')).replace('\\', '/')

The EM_CONFIG environment variable doesn't survive execution of this python 
script however.

> to point to emscripten directly via `EMSCRIPTEN_ROOT` ...

Isn't this a chicken-egg problem? The EMSCRIPTEN_ROOT variable isn't set 
anywhere on my machine, except after running the platform-specific 
emsdk_env.xxx scripts (which I don't usually do, except when I need emcc in 
the path for some reason). I'd need to run this script (or 'emsdk 
construct_env') of a specific emscripten SDK installation from inside 
python. But running emsdk from within Python which sets environment 
variables doesn't pass this new environment up to the Python process where 
the environment variables would be needed for invoking cmake (AFAIK 
environment variables can only be inherited from parent to child processes, 
but not the other way around).

All in all, the way the current Emscripten SDK works when activated as 
"--embedded" is pretty much perfect because everything already works 
without anything in the PATH or in environment variables. The only wonky 
part is how to get from the SDK installation directory (where 'emsdk' is 
located - which is under my control) to the 'compiler toolchain directory' 
which is needed by cmake (e.g. "[emsdk_dir]/upstream/emscripten"). I 
consider the current way to evaluate .emscripten inside python a hack, but 
it's a reasonably simple hack that works ;)

Any alternative should be simpler then that though ("emsdk construct_env" 
does a lot more things then just setting up 'EMSCRIPTEN_ROOT', and as I 
described above, doesn't really work for my use case because the modified 
environment is "lost" in a child process).

Phew :)
-Floh.

On Wednesday, 14 April 2021 at 08:16:57 UTC+2 s...@google.com wrote:

> On Tue, Apr 13, 2021 at 1:27 PM Floh  wrote:
>
>> Yeah, I never used python literals in EM_CONFIG, misunderstanding on my 
>> part :)
>>
>> About the other thing:
>>
>> > a) Use `which emcc` to find where emscripten is
>>
>> In my case, emcc is not in the path because this is a local installation 
>> (via "emsdk activate --embedded") without any traces in the environment 
>> (I'm setting EM_CONFIG only temporary in the "python environment" before 
>> evaluating .emscripten as a workaround.
>>
>>
> Is there some reason you don't want to add the emscripten directory to you 
> PATH?  Is that somehow worse than setting EM_CONFIG and then parsing it?
>
> > b) Have emsdk set `EMSCRIPTEN_ROOT` directly in the environment rather 
>> than in the config file. 
>>
>> ...this also isn't an option for the same reason as above, there is no 
>> "global emscripten installation" at all, and there may be multiple 
>> installations of the emscripten SDK with different versions side by side.
>>
>>
> I'm not sure I understand.  You say you are setting `EM_CONFIG` in your 
> environment according to your needs, so that there is not one global 
> `EM_CONFIG`.  Is there some reason you couldn't alternatively set 
> `EMSCRIPTEN_ROOT`?
>
> I don't see how either of these imply some "global emscripten 
> installation".  In both cases the current environment determins the 
> installation to use, no?.   In other words, would it be possible (in your 
> setup) to point to emscripten directly via `EMSCRIPTEN_ROOT` rather than 
> pointing at a config file via `EM_CONFIG` (and then parsing the config file 
> to find EMSCRIPTEN_ROOT)?  (it would then be up to the emcc executable to 
> find its own config file).
>
>  
>
>> Cmake takes are of making all paths (compiler tools, header search paths 
>> etc...) absolute starting at EMSCRIPTEN_ROOT (which cmake doesn't read from 
>> an environement variable but gets as command line argument, like cmake 
>> -DEMSCRIPTEN_ROOT=xxx.
>>
>> However as long as EMSCRIPTEN_ROOT is alw