Accessing C globals from JS (via NAMED_GLOBALS or EXPORTED_GLOBALS)

300 views
Skip to first unread message

David Foster

unread,
Nov 9, 2015, 11:19:28 PM11/9/15
to emscripten-discuss
I am attempting to build CPython 2.7 using jallwine's instructions at [1], however I am making one big change: I am using a modern version of Emscripten (1.35.0) rather than the old_shared_libs branch (which is circa May 2013). I have gotten pretty far, although I am stuck on one point: accessing global variables from JavaScript.

Emscripten@old_shared_libs used to support a "-s NAMED_GLOBALS=1" option, which makes globals in CPython such as "Py_NoSiteFlag" accessible from the JS top-level environment. After export I could write to such a global using code like "setValue(_Py_NoSiteFlag, 1, 'i32');". However in the latest version of Emscripten I am having trouble performing the same types of global variable accesses.

Emscripten@master (1.35.0) does not recognize the "-s NAMED_GLOBALS=1" option. It does however recognize `-s EXPORTED_GLOBALS="['_Py_NoSiteFlag']"`, which I'm guessing works similarly to the EXPORTED_FUNCTIONS option. Unfortunately adding the EXPORTED_GLOBALS flag to all the same compile steps as EXPORTED_FUNCTIONS does not appear to actually export any globals where I can find them. Certainly not at the `Module._Py_NoSiteFlag` location, which is what I'd expect.

I am unable to find any documentation about accessing C globals from JavaScript. In particular the guide documentation at [2] does not make any mention.

Is there any obvious I am doing wrong here? Is using EXPORTED_GLOBALS the right approach? If so, does anyone have a small program making use of EXPORTED_GLOBALS that I could examine to determine the appropriate usage, given the apparent lack of documentation?

- David

David Foster

unread,
Nov 10, 2015, 1:41:41 PM11/10/15
to emscripten-discuss
Odd. I am no longer able to find the mention of EXPORTED_GLOBALS in emscripten.h, which is where I think I learned about it. So perhaps that option does not in fact exist.

In the meantime I've worked around the inability to access globals directly by creating accessor functions that get/set the appropriate global variables. Then I've exported those accessors via EXPORTED_FUNCTIONS. Appears to work.

- David

Alon Zakai

unread,
Nov 10, 2015, 2:23:01 PM11/10/15
to emscripten-discuss
Sorry for the confusion here - yes, EXPORTED_GLOBALS is obsolete (it was only supported before fastcomp). I removed it from settings.js now.

Yes, the proper way to do this is to create global accessor functions, and export those.

--
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.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages