-
Notifications
You must be signed in to change notification settings - Fork 149
Experimental JS API based on tracking values #2013
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,8 +28,12 @@ add_subdirectory(../../../libAtomVM libAtomVM) | |
| target_link_libraries(AtomVM PUBLIC libAtomVM) | ||
| target_compile_options(libAtomVM PUBLIC -O3 -fno-exceptions -fno-rtti -pthread -sINLINING_LIMIT -sUSE_ZLIB=1) | ||
| target_compile_definitions(libAtomVM PRIVATE WITH_ZLIB) | ||
| target_link_options(AtomVM PRIVATE -sEXPORTED_RUNTIME_METHODS=ccall -sUSE_ZLIB=1 -O3 -pthread -sFETCH -lwebsocket.js --pre-js ${CMAKE_CURRENT_SOURCE_DIR}/atomvm.pre.js) | ||
|
|
||
| if (AVM_USE_WASM_MJS) | ||
| target_link_options(AtomVM PRIVATE -sEXPORTED_RUNTIME_METHODS=ccall,cwrap,stringToNewUTF8 -sEMULATE_FUNCTION_POINTER_CASTS=1 -sEXPORTED_FUNCTIONS=_malloc,_cast,_call,_next_tracked_object_key,_main -sEXPORT_ES6=1 -sUSE_ZLIB=1 -O3 -pthread -sFETCH -lwebsocket.js --pre-js ${CMAKE_CURRENT_SOURCE_DIR}/atomvm.pre.js) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. -O3 if a pretty aggressive optimization option. Do we really want to hardcode it?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. I vaguely recall that CMake has some standard compiler/linker env vars so we could use them. Either way, choice between I just retained current state which hardcodes
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need cwrap and stringToNewUTF8? |
||
| set(CMAKE_EXECUTABLE_SUFFIX ".mjs") | ||
| else() | ||
| target_link_options(AtomVM PRIVATE -sEXPORTED_RUNTIME_METHODS=ccall -sUSE_ZLIB=1 -O3 -pthread -sFETCH -lwebsocket.js --pre-js ${CMAKE_CURRENT_SOURCE_DIR}/atomvm.pre.js) | ||
| endif() | ||
| if (CMAKE_BUILD_TYPE STREQUAL "Debug") | ||
| target_link_options(AtomVM PRIVATE -sASSERTIONS=2 -sSAFE_HEAP -sSTACK_OVERFLOW_CHECK) | ||
| endif() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,10 +17,60 @@ | |
| * | ||
| * SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later | ||
| */ | ||
| Module['cast'] = function(name, message) { | ||
| ccall("cast", 'void', ['string', 'string'], [name, message]); | ||
| Module["cast"] = function (name, message) { | ||
| ccall("cast", "void", ["string", "string"], [name, message]); | ||
| }; | ||
| Module['call'] = async function(name, message) { | ||
| const promiseId = ccall("call", 'integer', ['string', 'string'], [name, message]); | ||
| return promiseMap.get(promiseId).promise; | ||
| Module["call"] = async function (name, message) { | ||
| const promiseId = ccall( | ||
| "call", | ||
| "integer", | ||
| ["string", "string"], | ||
| [name, message], | ||
| ); | ||
| return promiseMap.get(promiseId).promise; | ||
| }; | ||
|
|
||
| // experimental API | ||
| Module["nextTrackedObjectKey"] = function () { | ||
| return ccall("next_tracked_object_key", "integer", [], []); | ||
| }; | ||
| Module["trackedObjectsMap"] = new Map(); | ||
| Module["onTrackedObjectDelete"] = (key) => { | ||
| Module["trackedObjectsMap"].delete(key); | ||
| }; | ||
| Module["onGetTrackedObjects"] = (keys) => { | ||
| const getTrackedObject = (key) => Module["trackedObjectsMap"].get(key); | ||
| return keys.map(getTrackedObject); | ||
| }; | ||
| Module["onRunTrackedJs"] = (scriptString, isDebug) => { | ||
| const trackValue = (value) => { | ||
| const key = Module["nextTrackedObjectKey"](); | ||
| Module["trackedObjectsMap"].set(key, value); | ||
| return key; | ||
| }; | ||
|
|
||
| let result; | ||
| try { | ||
| const indirectEval = eval; | ||
| result = indirectEval(scriptString); | ||
| } catch (_e) { | ||
| return null; | ||
| } | ||
| isDebug && ensureValidResult(result); | ||
| return result?.map(trackValue) ?? []; | ||
| }; | ||
|
|
||
| function ensureValidResult(result) { | ||
| const isIndex = (k) => typeof k === "number"; | ||
|
|
||
| if (result === null) { | ||
| return; | ||
| } | ||
| if (Array.isArray(result) && keys.every(isIndex)) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure that keys exists. You probably mean |
||
| return; | ||
| } | ||
|
|
||
| const message = | ||
| "Evaluated script returned invalid value. Expected number array or null"; | ||
| throw new Error(message); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about this option. Should this become the default? What are the pros and cons?