Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

emscripten

30 views
Skip to first unread message

Marcello Stanisci

unread,
Jun 15, 2015, 9:07:52 AM6/15/15
to dev-w...@lists.mozilla.org
Hi All,

The scenario is a firefox extension that has to call some functions defined
in a JavaScript source file obtained by compiling some C code using Emscripten.

Putting the ported JavaScript in some location inside the bundle and loading
it as if it were a 'normal' code module, seems not to work (after all, Emscripten
is not forced to declare the needed 'EXPORTED_SYMBOLS' array inside its generated
files.)

In [1] is explaiend how to do that in content pages; the guess is that to do the same
in chrome code, the ported JavaScript should be 'injected' into the main page scope, that
is browser.xul.

In [2] are shown various ways to load a script, but nothing seems to inject the code in
browser.xul.


Any hints, suggestions, links on the topic will be appreciated.

Thanks in advance
Marcello

[1] https://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html#calling-compiled-c-functions-from-javascript
[2] https://developer.mozilla.org/en-US/Add-ons/Overlay_Extensions/XUL_School/Appendix_D%3A_Loading_Scripts

Marcello Stanisci

unread,
Jun 15, 2015, 9:09:07 AM6/15/15
to dev-w...@lists.mozilla.org
PS the extension is a restartless one.

Ehsan Akhgari

unread,
Jun 15, 2015, 9:45:52 AM6/15/15
to Marcello Stanisci, dev-w...@lists.mozilla.org
Hi Marcello,

(FWIW this question seems to be more appropriate for dev-platform.)

It's hard to say without seeing any sample code, but it seems like the
generated emscriptened code is placed inside a JSM file. Have you made
sure to export a wrapper function from the JSM through EXPORTED_SYMBOLS
that uses Module.ccall() to call the emscriptened function?
> _______________________________________________
> dev-webapi mailing list
> dev-w...@lists.mozilla.org
> https://lists.mozilla.org/listinfo/dev-webapi
>

Marcello Stanisci

unread,
Jun 15, 2015, 11:15:32 AM6/15/15
to Ehsan Akhgari, dev-w...@lists.mozilla.org
Hi Ehsan,

> (FWIW this question seems to be more appropriate for dev-platform.)
Ok, I'll take note for future postings.

> It's hard to say without seeing any sample code, but it seems like the
> generated emscriptened code is placed inside a JSM file. Have you made sure
> to export a wrapper function from the JSM through EXPORTED_SYMBOLS that uses
> Module.ccall() to call the emscriptened function?

Actually, placing the emscripten generated code inside a JSM would mean
putting inside it (that is approx. 200.000 lines of code), as you suggest,
the declaration of EXPORTED_SYMBOLS array (I've not seen any compile option
that would make that). Not to mention that emscripten runs various kinds of
optimizations that make the original code hard to recognize. Additionally,
the emscripted code needs to run in some 'window' context, thus 'packaging'
it as a JSM may not work.

Anyway, a seemingly working solution has been calling

'Services.scriptloader.loadSubScript("chrome://myaddon/content/emscripted.js", currentWindow)';

that way, to call 'Module.cwrap()' and 'Module.ccall()', it's necessary to prepend 'currentWindow',
as

'currentWindow.Module.cwrap()'
'currentWindow.Module.ccall()'


Thanks

Regards
Marcello

Ehsan Akhgari

unread,
Jun 15, 2015, 2:13:44 PM6/15/15
to Marcello Stanisci, dev-w...@lists.mozilla.org
On 2015-06-15 11:14 AM, Marcello Stanisci wrote:
> Hi Ehsan,
>
>> (FWIW this question seems to be more appropriate for dev-platform.)
> Ok, I'll take note for future postings.
>
>> It's hard to say without seeing any sample code, but it seems like the
>> generated emscriptened code is placed inside a JSM file. Have you made sure
>> to export a wrapper function from the JSM through EXPORTED_SYMBOLS that uses
>> Module.ccall() to call the emscriptened function?
>
> Actually, placing the emscripten generated code inside a JSM would mean
> putting inside it (that is approx. 200.000 lines of code), as you suggest,
> the declaration of EXPORTED_SYMBOLS array (I've not seen any compile option
> that would make that).

I was suggesting that you need to make the EXPORTED_SYMBOLS stuff
manually. But that is only because I thought you are using a JSM file
from your original email (that didn't really contain enough information
about what you're trying to do. ;-)

If I misunderstood, can you please explain exactly what you're trying to
do, and what breaks?

> Not to mention that emscripten runs various kinds of
> optimizations that make the original code hard to recognize. Additionally,
> the emscripted code needs to run in some 'window' context, thus 'packaging'
> it as a JSM may not work.
>
> Anyway, a seemingly working solution has been calling
>
> 'Services.scriptloader.loadSubScript("chrome://myaddon/content/emscripted.js", currentWindow)';
>
> that way, to call 'Module.cwrap()' and 'Module.ccall()', it's necessary to prepend 'currentWindow',
> as
>
> 'currentWindow.Module.cwrap()'
> 'currentWindow.Module.ccall()'

Yeah, that should work. But I still don't know what you're trying to
do, so that may or may not be the right solution.

Marcello Stanisci

unread,
Jun 16, 2015, 4:06:19 AM6/16/15
to Ehsan Akhgari, dev-w...@lists.mozilla.org
> If I misunderstood, can you please explain exactly what you're trying to do,
> and what breaks?

Sure! There is a C library libX that, compiled with emscripten, becomes libX.js.
I would like to be able to call libX.js's functions from within my firefox extension;
so it's needed a way to include libX.js into that extension's scope.

What breaks is the lack of EXPORTED_SYMBOLS inside libX.js and the fact that libX.js
has to be run inside a 'window' scope; and such a scope is not implicit inside a bootstrapped
extension's startup(). Additionally, even though we could obtain some 'window' object,
we can't instruct System.util.import(..) to run our script with that 'window' as its global
scope.

So loadSubScript(script, scope) fixes that since it takes among its arguments even the
scope we want our script to be run into.

HTH

In case you want to go on with that, feel free to write me without passing through
the list.

Marcello
0 new messages