Skip to content
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

fix: wasm deser #45

Merged
merged 2 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions Assets/Dojo/Plugins/WebGL/torii_c.jslib
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,19 @@ mergeInto(LibraryManager.library, {
var idsString = UTF8ToString(ids);
var idsArray = JSON.parse(idsString);

const subscription = await client.onEventMessageUpdated(idsArray, (entities) => {
// stringify the entities
var entitiesString = JSON.stringify(entities);
// return buffer
var bufferSize = lengthBytesUTF8(entitiesString) + 1;
var buffer = _malloc(bufferSize);
stringToUTF8(entitiesString, buffer, bufferSize);

dynCall_vi(cb, buffer);
});
const subscription = await client.onEventMessageUpdated(
idsArray,
(entities) => {
// stringify the entities
var entitiesString = JSON.stringify(entities);
// return buffer
var bufferSize = lengthBytesUTF8(entitiesString) + 1;
var buffer = _malloc(bufferSize);
stringToUTF8(entitiesString, buffer, bufferSize);

dynCall_vi(cb, buffer);
}
);
subscription.__destroy_into_raw();

client.__destroy_into_raw();
Expand Down Expand Up @@ -163,7 +166,10 @@ mergeInto(LibraryManager.library, {
// signature: JSON string { r: string, s: string }
PublishMessage: async function (clientPtr, message, signature, cb) {
var client = wasm_bindgen.Client.__wrap(clientPtr);
const published = await client.publishMessage(UTF8ToString(message), JSON.parse(UTF8ToString(signature)));
const published = await client.publishMessage(
UTF8ToString(message),
JSON.parse(UTF8ToString(signature))
);
const publishedString = JSON.stringify(Array.from(published));
const bufferSize = lengthBytesUTF8(publishedString) + 1;
const buffer = _malloc(bufferSize);
Expand Down
2 changes: 1 addition & 1 deletion Assets/Dojo/Runtime/Config/WorldManagerLocalConfig.asset
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ MonoBehaviour:
toriiUrl: http://localhost:8080
rpcUrl: http://localhost:5050
relayUrl: /ip4/127.0.0.1/tcp/9090
relayWebrtcUrl: /ip4/127.0.0.1/udp/9091/webrtc-direct/certhash/uEiDMwVv8g645xRcfxVd25Yveps7p3mtnJqlSwQpMLib4RA
relayWebrtcUrl: /ip4/127.0.0.1/udp/9091/webrtc-direct/certhash/uEiAS9CpA5yNwO7iidBM5f9FcPl67PwohiK-1J9BhnFMrHg
limit: 100
worldAddress: 0x2e31cfde9f9990c7fe44b25043e3c6958a849c0a66ab535686d2b710e97f309
2 changes: 0 additions & 2 deletions Assets/Dojo/Runtime/Starknet/TypedData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
using System.Linq;
using System.Numerics;
using bottlenoselabs.C2CS.Runtime;
using Codice.CM.SEIDInfo;
using Dojo.Torii;
using dojo_bindings;
using Newtonsoft.Json;
using PlasticGui;

namespace Dojo.Starknet
{
Expand Down
54 changes: 29 additions & 25 deletions Assets/Dojo/Runtime/Torii/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,37 +99,41 @@ private Ty HandleWasmValue(WasmValue value)
return value.type switch
{
// struct
"struct" => new Ty(dojo.Ty_Tag.Struct_, value.value.ToObject<WasmStruct>().name, HandleJSStruct(value.value.ToObject<WasmStruct>()), value.key),
"struct" => new Ty(dojo.Ty_Tag.Struct_, value.type_name, HandleJSStruct(value.value.ToObject<Dictionary<string, WasmValue>>()), value.key),
// enum
"enum" => new Ty(dojo.Ty_Tag.Enum_, value.value.ToObject<WasmEnum>().name, HandleJSEnum(value.value.ToObject<WasmEnum>()), value.key),
"enum" => new Ty(dojo.Ty_Tag.Enum_, value.type_name, HandleJSEnum(value.value.ToObject<WasmEnum>()), value.key),
// tuple
"tuple" => new Ty(dojo.Ty_Tag.Tuple_, "tuple", value.value.ToObject<JArray>().Select(m => HandleWasmValue(m.ToObject<WasmValue>())).ToArray(), value.key),
// array
"array" => new Ty(dojo.Ty_Tag.Array_, "array", value.value.ToObject<JArray>().Select(m => HandleWasmValue(m.ToObject<WasmValue>())).ToList(), value.key),
// primitives
"bool" => new Ty(dojo.Ty_Tag.Primitive_, "bool", value.value.ToObject<bool>(), value.key),
"u8" => new Ty(dojo.Ty_Tag.Primitive_, "u8", value.value.ToObject<byte>(), value.key),
"u16" => new Ty(dojo.Ty_Tag.Primitive_, "u16", value.value.ToObject<ushort>(), value.key),
"u32" => new Ty(dojo.Ty_Tag.Primitive_, "u32", value.value.ToObject<uint>(), value.key),
"u64" => new Ty(dojo.Ty_Tag.Primitive_, "u64", value.value.ToObject<ulong>(), value.key),
// NOTE: UNTESTED
// NOTE: slow?
// use BigInteger parse instead maybe but seems a bit
// uninconvenient to use
"u128" => new Ty(dojo.Ty_Tag.Primitive_, "u128", new BigInteger(hexStringToByteArray(value.value.ToObject<string>()).Reverse().ToArray()), value.key),
// convert a 64 character hex string to a BigInteger
// IMPLEMNET
"u256" => new Ty(dojo.Ty_Tag.Primitive_, "u256", new Dictionary<string, object>(){
"bytearray" => new Ty(dojo.Ty_Tag.ByteArray, "bytearray", value.value.ToObject<string>(), value.key),
"primitive" => value.type_name switch
{
// primitives
"bool" => new Ty(dojo.Ty_Tag.Primitive_, "bool", value.value.ToObject<bool>(), value.key),
"u8" => new Ty(dojo.Ty_Tag.Primitive_, "u8", value.value.ToObject<byte>(), value.key),
"u16" => new Ty(dojo.Ty_Tag.Primitive_, "u16", value.value.ToObject<ushort>(), value.key),
"u32" => new Ty(dojo.Ty_Tag.Primitive_, "u32", value.value.ToObject<uint>(), value.key),
"u64" => new Ty(dojo.Ty_Tag.Primitive_, "u64", value.value.ToObject<ulong>(), value.key),
// NOTE: UNTESTED
// NOTE: slow?
// use BigInteger parse instead maybe but seems a bit
// uninconvenient to use
"u128" => new Ty(dojo.Ty_Tag.Primitive_, "u128", new BigInteger(hexStringToByteArray(value.value.ToObject<string>()).Reverse().ToArray()), value.key),
// convert a 64 character hex string to a BigInteger
// IMPLEMNET
"u256" => new Ty(dojo.Ty_Tag.Primitive_, "u256", new Dictionary<string, object>(){
{"high", new BigInteger(hexStringToByteArray(value.value.ToObject<string>().Substring(0, 32)).Reverse().ToArray())},
{"low", new BigInteger(hexStringToByteArray(value.value.ToObject<string>().Substring(32, 32)).Reverse().ToArray())}
}, value.key),
"usize" => new Ty(dojo.Ty_Tag.Primitive_, "usize", value.value.ToObject<uint>(), value.key),
// these should be fine
"felt252" => new Ty(dojo.Ty_Tag.Primitive_, "felt252", new FieldElement(value.value.ToObject<string>()), value.key),
"class_hash" => new Ty(dojo.Ty_Tag.Primitive_, "class_hash", new FieldElement(value.value.ToObject<string>()), value.key),
"contract_address" => new Ty(dojo.Ty_Tag.Primitive_, "contract_address", new FieldElement(value.value.ToObject<string>()), value.key),
"bytearray" => new Ty(dojo.Ty_Tag.ByteArray, "bytearray", value.value.ToObject<string>(), value.key),
_ => throw new Exception("Unknown primitive type")
"usize" => new Ty(dojo.Ty_Tag.Primitive_, "usize", value.value.ToObject<uint>(), value.key),
// these should be fine
"felt252" => new Ty(dojo.Ty_Tag.Primitive_, "felt252", new FieldElement(value.value.ToObject<string>()), value.key),
"class_hash" => new Ty(dojo.Ty_Tag.Primitive_, "class_hash", new FieldElement(value.value.ToObject<string>()), value.key),
"contract_address" => new Ty(dojo.Ty_Tag.Primitive_, "contract_address", new FieldElement(value.value.ToObject<string>()), value.key),
_ => throw new Exception("Unknown primitive type: " + value.type_name)
},
_ => throw new Exception("Unknown type: " + value.type)
};
}

Expand All @@ -156,9 +160,9 @@ private Enum HandleCEnum(dojo.Enum en)
return new Enum(option.name, HandleCValue(option.ty, false));
}

private Dictionary<string, Ty> HandleJSStruct(WasmStruct str)
private Dictionary<string, Ty> HandleJSStruct(Dictionary<string, WasmValue> str)
{
return str.children.Select(m => new KeyValuePair<string, Ty>(m.Key, HandleWasmValue(m.Value))).ToDictionary(k => k.Key, v => v.Value);
return str.Select(m => new KeyValuePair<string, Ty>(m.Key, HandleWasmValue(m.Value))).ToDictionary(k => k.Key, v => v.Value);
}

private Enum HandleJSEnum(WasmEnum en)
Expand Down
9 changes: 1 addition & 8 deletions Assets/Dojo/Runtime/Torii/ToriiWasmInterop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,18 @@ namespace Dojo.Torii
public struct WasmValue
{
public string type;
public string type_name;
public JToken value;
public bool key;
}

[Serializable]
public struct WasmEnum
{
public string name;
public string option;
public WasmValue value;
}

[Serializable]
public struct WasmStruct
{
public string name;
public Dictionary<string, WasmValue> children;
}

public class ToriiWasmInterop : MonoBehaviour
{
// Creates a new client and returns the pointer to it
Expand Down
2 changes: 1 addition & 1 deletion Assets/Spawn And Move/Contracts/Actions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public async Task<FieldElement> dojo_init(Account account) {
public async Task<FieldElement> spawn(Account account) {
List<dojo.FieldElement> calldata = new List<dojo.FieldElement>();


return await account.ExecuteRaw(new dojo.Call[] {
new dojo.Call{
to = contractAddress,
Expand Down
12 changes: 6 additions & 6 deletions Assets/WebGLTemplates/Dojo/TemplateData/dojo.js/dojo_c.js
Original file line number Diff line number Diff line change
Expand Up @@ -1712,27 +1712,27 @@ function __wbg_get_imports() {
const ret = wasm.memory;
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper2642 = function(arg0, arg1, arg2) {
imports.wbg.__wbindgen_closure_wrapper2640 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 859, __wbg_adapter_30);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper2643 = function(arg0, arg1, arg2) {
imports.wbg.__wbindgen_closure_wrapper2641 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 859, __wbg_adapter_30);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper2644 = function(arg0, arg1, arg2) {
imports.wbg.__wbindgen_closure_wrapper2642 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 859, __wbg_adapter_30);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper4312 = function(arg0, arg1, arg2) {
imports.wbg.__wbindgen_closure_wrapper4310 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 1644, __wbg_adapter_37);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper5141 = function(arg0, arg1, arg2) {
imports.wbg.__wbindgen_closure_wrapper5139 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 1946, __wbg_adapter_40);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper5851 = function(arg0, arg1, arg2) {
imports.wbg.__wbindgen_closure_wrapper5849 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 2271, __wbg_adapter_43);
return addHeapObject(ret);
};
Expand Down
Binary file modified Assets/WebGLTemplates/Dojo/TemplateData/dojo.js/dojo_c_bg.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion Bindings/dojo.c
Submodule dojo.c updated 1 files
+2 −4 src/wasm/utils.rs