Skip to content

Instantly share code, notes, and snippets.

@uyjulian
Last active January 6, 2022 16:54
Show Gist options
  • Save uyjulian/66bb0466136d04bc1920d55894edb8c0 to your computer and use it in GitHub Desktop.
Save uyjulian/66bb0466136d04bc1920d55894edb8c0 to your computer and use it in GitHub Desktop.

TJS<->JS interface information

JS side

Module.evalTJS

Evaluates passed in argument as TJS expression.
Example:

Module.evalTJS('Debug.message(\"Hello world from TJS!\")');

Module.getLayerBitmapUInt8Array

Gets Uint8Array object mapped inside memory buffer of the Layer or Bitmap object.
The data is mapped inside of the C++ environment using HEAP.U8 array.
Second argument should be true if the buffer is to be written to.
Example:

var buf = Module.getLayerBitmapUInt8Array(layer_object, true);
buf[0] = 0xff;
buf[1] = 0xff;
buf[2] = 0xff;
buf[3] = 0xff;

Module.getStorageUInt8Array

Gets Uint8Array object of a file in the TVP storage.
The data will be copied and is not mapped inside of the C++ environment.
Example:

var buf = Module.getStorageUInt8Array("file://./data.xp3>in_storage_data.bin");

Module.getStorageString

Gets String of a file in the TVP storage.
The second argument is the mode string. The syntax of the mode string is the same as e.g. Array.load.
Example:

var str = Module.getStorageString("file://./data.xp3>in_storage_data.txt", "");
console.log(str);

TJS side

KirikiriEmscriptenInterface.evalJS

Evaluates the passed in string as JS.
Example:

KirikiriEmscriptenInterface.evalJS('console.log(\"Hello world from JS!\")');

KirikiriEmscriptenInterface.evalStorageJS

Evaluates the passed in storage as JS.
Second argument is the mode string and is optional.
Example:

KirikiriEmscriptenInterface.evalStorageJS('file://./data.xp3>in_storage_data.js', '');

Type conversion

JS -> TJS

  • boolean -> Integer
  • bigint -> Integer
  • number -> Real
  • string -> String
  • function -> wrapped Object
  • object -> wrapped Object

TJS -> JS

  • Integer -> BigInt
  • Real -> number
  • String -> string
  • Octet -> mapped Uint8Array
  • Object -> wrapped function or object (depending on the result of lhs instanceof "Function") using Proxy object

Note about wrapping objects

Due to JS not supporting destructors, passing an object from TJS to JS will cause a memory leak, so it is recommended to pass objects as little as possible.
JS to TJS objects will be reference counted properly as usual.

TJS->JS wrapped object support

  • (apply) Call Object instanceof Function: x(arg1, arg2)
  • (construct) Construct Object: new x(arg1, arg2)
  • (get) Get property by string or number: x["hoge"] or x[1]
  • (set) Set property by string or number: x["hoge"] = 1 or x[1] = 1
  • (has) Has property by string or number: "hoge" in x or 1 in x
  • (deleteProperty) Delete property by string or number: delete x["hoge"]; or delete x[1];
  • (ownKeys) List properties: Reflect.ownKeys(x)

JS->TJS wrapped object support

  • (FuncCall, FuncCallByNum) Call object or function: x(arg1, arg2)
  • (CreateNew) Construct object or function: new x(arg1, arg2)
  • (PropGet, PropGetByNum) Get property by string or number: x["hoge"] or x[1]
  • (PropSet, PropSetByNum) Set property by string or number: x["hoge"] = 1 or x[1] = 1
  • (DeleteMember, DeleteMemberByNum) Delete property by string or number: delete x["hoge"]; or delete x[1];
  • (EnumMembers) Enumerate properties (not exposed directly in TJS)

Exception handling

Object and String values can be thrown.
At the moment, throwing Integer and Real values cause undefined behavior.
When throwing TJS Object values across the JS<->TJS barrier, they will be converted to string value.
When throwing JS string or object values across the JS<->TJS barrier, they will be converted to TJS Object value of class Exception.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment