Skip to content

Commit

Permalink
Changes over to use a 64-bit API.
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertBaruch committed Jan 8, 2024
1 parent 33367c6 commit bc0af85
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 87 deletions.
12 changes: 2 additions & 10 deletions dergwasm_mod/Dergwasm/EmscriptenEnv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -703,16 +703,8 @@ int adjusted_ptrPtr
// This was present in hello_world.c. It returns a long, but the actual return value
// is just the low 32 bits. The upper 32 bits get stored in $global1 (although we don't
// yet support exported globals).
public int dynCall_jiji(Frame frame, int index, int a, int b_lo, int b_hi, int d) =>
CallExportedFunc<int, int, int, int, int, int>(
"dynCall_jiji",
frame,
index,
a,
b_lo,
b_hi,
d
);
public long dynCall_jiji(Frame frame, int index, int a, long b, int d) =>
CallExportedFunc<long, int, int, long, int>("dynCall_jiji", frame, index, a, b, d);

// The various dyncall_* functions for setjmp/longjmp.
// The first character is the return type, and the rest are the arg types.
Expand Down
6 changes: 3 additions & 3 deletions dergwasm_mod/Dergwasm/EmscriptenWasi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void RegisterHostFuncs()
"fd_write",
FdWrite
);
machine.RegisterReturningHostFunc<int, int, int, int, int, int>(
machine.RegisterReturningHostFunc<int, long, int, int, int>(
"wasi_snapshot_preview1",
"fd_seek",
FdSeek
Expand Down Expand Up @@ -162,10 +162,10 @@ int FdWrite(Frame frame, int fd, int iov, int iovcnt, int pnum)
//
// Returns:
// 0 on success, or -ERRNO on failure.
int FdSeek(Frame frame, int fd, int offset_lo, int offset_hi, int whence, int newOffsetPtr)
int FdSeek(Frame frame, int fd, long offset, int whence, int newOffsetPtr)
{
throw new Trap(
$"Unimplemented call to FdSeek({fd}, {offset_lo}, {offset_hi}, {whence}, 0x{newOffsetPtr:X8})"
$"Unimplemented call to FdSeek({fd}, {offset}, {whence}, 0x{newOffsetPtr:X8})"
);
}

Expand Down
79 changes: 31 additions & 48 deletions dergwasm_mod/Dergwasm/ResoniteEnv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ public ResoniteEnv(Machine machine, World world, EmscriptenEnv emscriptenEnv)

public Slot SlotFromRefID(uint slot_id_lo, uint slot_id_hi)
{
RefID refID = new RefID(((ulong)slot_id_hi << 32) | slot_id_lo);
return SlotFromRefID(((ulong)slot_id_hi << 32) | slot_id_lo);
}

public Slot SlotFromRefID(ulong slot_id)
{
RefID refID = new RefID(slot_id);
return world.ReferenceController.GetObjectOrNull(refID) as Slot;
}

Expand Down Expand Up @@ -152,97 +157,75 @@ public void CallWasmFunction(Slot argsSlot)
// This only needs to be called once, when the WASM Machine is initialized and loaded.
public void RegisterHostFuncs()
{
machine.RegisterVoidHostFunc<uint>("env", "slot__root_slot", slot__root_slot);
machine.RegisterVoidHostFunc<uint, uint, uint>(
machine.RegisterReturningHostFunc<ulong>("env", "slot__root_slot", slot__root_slot);
machine.RegisterReturningHostFunc<ulong, ulong>(
"env",
"slot__get_parent",
slot__get_parent
);
machine.RegisterVoidHostFunc<uint, uint, uint>(
machine.RegisterReturningHostFunc<ulong, ulong>(
"env",
"slot__get_active_user",
slot__get_active_user
);
machine.RegisterVoidHostFunc<uint, uint, uint>(
machine.RegisterReturningHostFunc<ulong, ulong>(
"env",
"slot__get_active_user_root",
slot__get_active_user_root
);
machine.RegisterVoidHostFunc<uint, uint, int, uint>(
machine.RegisterReturningHostFunc<ulong, int, ulong>(
"env",
"slot__get_object_root",
slot__get_object_root
);
machine.RegisterReturningHostFunc<uint, uint, int>(
"env",
"slot__get_name",
slot__get_name
);
machine.RegisterReturningHostFunc<ulong, int>("env", "slot__get_name", slot__get_name);
machine.RegisterVoidHostFunc<ulong, int>("env", "slot__set_name", slot__set_name);
}

//
// The host functions. They are always called from WASM, so they already have a frame.
//

public void slot__root_slot(Frame frame, uint rootPtr)
public ulong slot__root_slot(Frame frame)
{
Slot slot = world.RootSlot;
machine.MemSet(rootPtr, (ulong)slot.ReferenceID);
return (ulong)slot.ReferenceID;
}

public void slot__get_parent(Frame frame, uint slot_id_lo, uint slot_id_hi, uint parentPtr)
public ulong slot__get_parent(Frame frame, ulong slot_id)
{
Slot slot = SlotFromRefID(slot_id_lo, slot_id_hi);
machine.MemSet(parentPtr, ((ulong?)slot?.Parent?.ReferenceID) ?? 0);
Slot slot = SlotFromRefID(slot_id);
return ((ulong?)slot?.Parent?.ReferenceID) ?? 0;
}

public void slot__get_active_user(
Frame frame,
uint slot_id_lo,
uint slot_id_hi,
uint userPtr
)
public ulong slot__get_active_user(Frame frame, ulong slot_id)
{
Slot slot = SlotFromRefID(slot_id_lo, slot_id_hi);
machine.MemSet(userPtr, ((ulong?)slot?.ActiveUser?.ReferenceID) ?? 0);
Slot slot = SlotFromRefID(slot_id);
return ((ulong?)slot?.ActiveUser?.ReferenceID) ?? 0;
}

public void slot__get_active_user_root(
Frame frame,
uint slot_id_lo,
uint slot_id_hi,
uint userRootPtr
)
public ulong slot__get_active_user_root(Frame frame, ulong slot_id)
{
Slot slot = SlotFromRefID(slot_id_lo, slot_id_hi);
machine.MemSet(userRootPtr, ((ulong?)slot?.ActiveUserRoot?.ReferenceID) ?? 0);
Slot slot = SlotFromRefID(slot_id);
return ((ulong?)slot?.ActiveUserRoot?.ReferenceID) ?? 0;
}

public void slot__get_object_root(
Frame frame,
uint slot_id_lo,
uint slot_id_hi,
int only_explicit,
uint objectRootPtr
)
public ulong slot__get_object_root(Frame frame, ulong slot_id, int only_explicit)
{
Slot slot = SlotFromRefID(slot_id_lo, slot_id_hi);
machine.MemSet(
objectRootPtr,
((ulong?)slot?.GetObjectRoot(only_explicit != 0)?.ReferenceID) ?? 0
);
Slot slot = SlotFromRefID(slot_id);
return ((ulong?)slot?.GetObjectRoot(only_explicit != 0)?.ReferenceID) ?? 0;
}

public int slot__get_name(Frame frame, uint slot_id_lo, uint slot_id_hi)
public int slot__get_name(Frame frame, ulong slot_id)
{
Slot slot = SlotFromRefID(slot_id_lo, slot_id_hi);
Slot slot = SlotFromRefID(slot_id);
string name = slot?.Name ?? "";
return emscriptenEnv.AllocateUTF8StringInMem(frame, name);
}

public void slot__set_name(Frame frame, uint slot_id_lo, uint slot_id_hi, int ptr)
public void slot__set_name(Frame frame, ulong slot_id, int ptr)
{
Slot slot = SlotFromRefID(slot_id_lo, slot_id_hi);
Slot slot = SlotFromRefID(slot_id);
if (slot == null)
return;
slot.Name = emscriptenEnv.GetUTF8StringFromMem(ptr);
Expand Down
Binary file modified firmware.wasm
Binary file not shown.
2 changes: 2 additions & 0 deletions usercmodule/resonite/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ make V=1 USER_C_MODULES=../../user_modules

See [micropython-usermod](https://micropython-usermod.readthedocs.io/) (slightly out of date) and
[MicroPython external C modules](https://docs.micropython.org/en/latest/develop/cmodules.html).

NOTE: The API uses 64-bit values. For Emscripten, this means you must compile with `-s WASM_BIGINT`.
1 change: 1 addition & 0 deletions usercmodule/resonite/micropython.mk
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ SRC_USERMOD += $(USERMODULES_DIR)/mp_resonite_slot.c
CFLAGS_USERMOD += -I$(USERMODULES_DIR)

JSFLAGS += --js-library $(USERMODULES_DIR)/resonite_api.js
JSFLAGS += -s WASM_BIGINT
12 changes: 3 additions & 9 deletions usercmodule/resonite/mp_resonite_slot_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,12 @@ mp_obj_t resonite_Slot_make_new(const mp_obj_type_t *type, size_t n_args, size_t
}

mp_obj_t resonite_Slot_root_slot(mp_obj_t cls_in) {
resonite_slot_refid_t id;
slot__root_slot(&id);
return resonite_create_slot(id);
return resonite_create_slot(slot__root_slot());
}

mp_obj_t resonite_Slot_get_parent(mp_obj_t self_in) {
resonite_Slot_obj_t *self = MP_OBJ_TO_PTR(self_in);
resonite_slot_refid_t id;
slot__get_parent(self->reference_id, &id);
return resonite_create_slot(id);
return resonite_create_slot(slot__get_parent(self->reference_id));
}

mp_obj_t resonite_Slot_get_object_root(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
Expand All @@ -49,9 +45,7 @@ mp_obj_t resonite_Slot_get_object_root(size_t n_args, const mp_obj_t *pos_args,
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
int only_explicit = args[0].u_bool ? 1 : 0;

resonite_slot_refid_t id;
slot__get_object_root(self->reference_id, only_explicit, &id);
return resonite_create_slot(id);
return resonite_create_slot(slot__get_object_root(self->reference_id, only_explicit));
}

mp_obj_t resonite_Slot_get_name(mp_obj_t self_in) {
Expand Down
16 changes: 8 additions & 8 deletions usercmodule/resonite/resonite_slot_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@
#include <stdint.h>
#include <emscripten.h>

EMSCRIPTEN_KEEPALIVE void _slot__root_slot(resonite_slot_refid_t* slot_id) {
slot__root_slot(slot_id);
EMSCRIPTEN_KEEPALIVE resonite_slot_refid_t _slot__root_slot() {
return slot__root_slot();
}

EMSCRIPTEN_KEEPALIVE void _slot__get_object_root(
resonite_slot_refid_t slot_id, int only_explicit, resonite_slot_refid_t* object_root_id) {
slot__get_object_root(slot_id, only_explicit, object_root_id);
EMSCRIPTEN_KEEPALIVE resonite_slot_refid_t _slot__get_object_root(
resonite_slot_refid_t slot_id, int only_explicit) {
return slot__get_object_root(slot_id, only_explicit);
}

EMSCRIPTEN_KEEPALIVE void _slot__get_parent(
resonite_slot_refid_t slot_id, resonite_slot_refid_t* parent_slot_id) {
slot__get_parent(slot_id, parent_slot_id);
EMSCRIPTEN_KEEPALIVE resonite_slot_refid_t _slot__get_parent(
resonite_slot_refid_t slot_id) {
return slot__get_parent(slot_id);
}

EMSCRIPTEN_KEEPALIVE char* _slot__get_name(resonite_slot_refid_t slot_id) {
Expand Down
16 changes: 7 additions & 9 deletions usercmodule/resonite/resonite_slot_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,33 @@

#include "resonite_api_types.h"

extern void slot__root_slot(resonite_slot_refid_t* slot_id);
extern resonite_slot_refid_t slot__root_slot();

// Returns the active user for the given slot.
//
// ProtoFlux equivalent: Users/GetActiveUser, Slots/GetActiveUser
// FrooxEngine equivalent: Slot.ActiveUser
extern void slot__get_active_user(resonite_slot_refid_t slot_id, resonite_user_refid_t* user_id);
extern resonite_user_refid_t slot__get_active_user(resonite_slot_refid_t slot_id);

// Returns the active user root for the given slot.
//
// ProtoFlux equivalent: Users/GetActiveUserRoot, Slots/GetActiveUserRoot
// FrooxEngine equivalent: Slot.ActiveUserRoot
extern void slot__get_active_user_root(
resonite_slot_refid_t slot_id,
resonite_user_root_refid_t* user_root_id);
extern resonite_user_root_refid_t slot__get_active_user_root(
resonite_slot_refid_t slot_id);

// Returns the object root for the given slot.
//
// ProtoFlux equivalent: Slots/GetObjectRoot
// FrooxEngine equivalent: Slot.GetObjectRoot
extern void slot__get_object_root(
resonite_slot_refid_t slot_id, int only_explicit, resonite_slot_refid_t* object_root_id);
extern resonite_slot_refid_t slot__get_object_root(
resonite_slot_refid_t slot_id, int only_explicit);

// Returns the parent slot for the given slot.
//
// ProtoFlux equivalent: Slots/GetParentSlot
// FrooxEngine equivalent: Slot.Parent
extern void slot__get_parent(
resonite_slot_refid_t slot_id, resonite_slot_refid_t* parent_slot_id);
extern resonite_slot_refid_t slot__get_parent(resonite_slot_refid_t slot_id);

// Returns the name for the given slot. The returned name must be freed when
// done with it. Although the return type is not const, changes to the returned
Expand Down

0 comments on commit bc0af85

Please sign in to comment.