From ed8d12d0e1dea8becde455f7a90d9acba819c5a6 Mon Sep 17 00:00:00 2001 From: Nasr Date: Mon, 22 Sep 2025 09:47:15 -0400 Subject: [PATCH 1/4] fix: virtual memory map for wasm refs --- Assets/Dojo/Plugins/WebGL/torii_c.jslib | 207 ++++++++++++++++++------ 1 file changed, 154 insertions(+), 53 deletions(-) diff --git a/Assets/Dojo/Plugins/WebGL/torii_c.jslib b/Assets/Dojo/Plugins/WebGL/torii_c.jslib index 18b93565..92508bb2 100644 --- a/Assets/Dojo/Plugins/WebGL/torii_c.jslib +++ b/Assets/Dojo/Plugins/WebGL/torii_c.jslib @@ -1,3 +1,9 @@ +// Global objects to store client instances and subscriptions +var toriiClients = {}; +var toriiSubscriptions = {}; +var nextClientId = 1; +var nextSubscriptionId = 1; + mergeInto(LibraryManager.library, { // Creates a new client and returns the pointer to it CreateClient: async function ( @@ -10,11 +16,19 @@ mergeInto(LibraryManager.library, { worldAddress: UTF8ToString(worldAddress), })); - dynCall_vi(cb, client.__destroy_into_raw()); + // Store client in global object and return virtual pointer + let clientId = nextClientId++; + toriiClients[clientId] = client; + dynCall_vi(cb, clientId); }, // Returns a page of all controllers GetControllers: async function (clientPtr, queryString, cb) { - const client = wasm_bindgen.ToriiClient.__wrap(clientPtr); + const client = toriiClients[clientPtr]; + if (!client) { + console.error('Client not found for ID:', clientPtr); + return; + } + const query = JSON.parse(UTF8ToString(queryString)); const controllersPage = await client.getControllers(query); @@ -23,12 +37,16 @@ mergeInto(LibraryManager.library, { const buffer = _malloc(bufferSize); stringToUTF8(controllersPageString, buffer, bufferSize); - client.__destroy_into_raw(); dynCall_vi(cb, buffer); }, // Returns a page of all tokens GetTokens: async function (clientPtr, queryString, cb) { - const client = wasm_bindgen.ToriiClient.__wrap(clientPtr); + const client = toriiClients[clientPtr]; + if (!client) { + console.error('Client not found for ID:', clientPtr); + return; + } + const query = JSON.parse(UTF8ToString(queryString)); const tokensPage = await client.getTokens(query); @@ -37,12 +55,16 @@ mergeInto(LibraryManager.library, { const buffer = _malloc(bufferSize); stringToUTF8(tokensPageString, buffer, bufferSize); - client.__destroy_into_raw(); dynCall_vi(cb, buffer); }, // Returns a page of all token balances GetTokenBalances: async function (clientPtr, queryString, cb) { - const client = wasm_bindgen.ToriiClient.__wrap(clientPtr); + const client = toriiClients[clientPtr]; + if (!client) { + console.error('Client not found for ID:', clientPtr); + return; + } + const query = JSON.parse(UTF8ToString(queryString)); const balancesPage = await client.getTokenBalances(query); @@ -51,12 +73,16 @@ mergeInto(LibraryManager.library, { const buffer = _malloc(bufferSize); stringToUTF8(balancesPageString, buffer, bufferSize); - client.__destroy_into_raw(); dynCall_vi(cb, buffer); }, // Returns a dictionary of all of the entities GetEntities: async function (clientPtr, queryString, cb) { - const client = wasm_bindgen.ToriiClient.__wrap(clientPtr); + const client = toriiClients[clientPtr]; + if (!client) { + console.error('Client not found for ID:', clientPtr); + return; + } + const query = JSON.parse(UTF8ToString(queryString)); let entitiesPage = await client.getEntities(query); @@ -67,12 +93,16 @@ mergeInto(LibraryManager.library, { let buffer = _malloc(bufferSize); stringToUTF8(entitiesPageString, buffer, bufferSize); - client.__destroy_into_raw(); dynCall_vi(cb, buffer); }, // Returns a dictionary of all of the eventmessages GetEventMessages: async function (clientPtr, queryString, cb) { - const client = wasm_bindgen.ToriiClient.__wrap(clientPtr); + const client = toriiClients[clientPtr]; + if (!client) { + console.error('Client not found for ID:', clientPtr); + return; + } + const query = JSON.parse(UTF8ToString(queryString)); let eventMessagesPage = await client.getEventMessages(query); @@ -83,12 +113,16 @@ mergeInto(LibraryManager.library, { let buffer = _malloc(bufferSize); stringToUTF8(eventMessagesPageString, buffer, bufferSize); - client.__destroy_into_raw(); dynCall_vi(cb, buffer); }, // Get the value of a model for a specific set of keys GetModelValue: async function (clientPtr, model, keys, cb) { - let client = wasm_bindgen.ToriiClient.__wrap(clientPtr); + const client = toriiClients[clientPtr]; + if (!client) { + console.error('Client not found for ID:', clientPtr); + return; + } + let modelValue = await client.getModelValue( UTF8ToString(model), JSON.parse(UTF8ToString(keys)) @@ -101,11 +135,15 @@ mergeInto(LibraryManager.library, { let buffer = _malloc(bufferSize); stringToUTF8(modelValueString, buffer, bufferSize); - client.__destroy_into_raw(); dynCall_vi(cb, buffer); }, OnTokenUpdated: async function (clientPtr, contractAddresses, tokenIds, cb, subCb) { - const client = wasm_bindgen.ToriiClient.__wrap(clientPtr); + const client = toriiClients[clientPtr]; + if (!client) { + console.error('Client not found for ID:', clientPtr); + return; + } + const subscription = await client.onTokenUpdated( JSON.parse(UTF8ToString(contractAddresses)), JSON.parse(UTF8ToString(tokenIds)), @@ -115,15 +153,21 @@ mergeInto(LibraryManager.library, { const buffer = _malloc(bufferSize); stringToUTF8(tokenString, buffer, bufferSize); - client.__destroy_into_raw(); - dynCall_vi(cb, buffer); - }); + dynCall_vi(cb, buffer); + }); - client.__destroy_into_raw(); - dynCall_vi(subCb, subscription.__destroy_into_raw()); + // Store subscription in global object and return virtual pointer + let subscriptionId = nextSubscriptionId++; + toriiSubscriptions[subscriptionId] = subscription; + dynCall_vi(subCb, subscriptionId); }, OnTokenBalanceUpdated: async function (clientPtr, contractAddresses, accountAddresses, tokenIds, cb, subCb) { - const client = wasm_bindgen.ToriiClient.__wrap(clientPtr); + const client = toriiClients[clientPtr]; + if (!client) { + console.error('Client not found for ID:', clientPtr); + return; + } + const subscription = await client.onTokenBalanceUpdated( JSON.parse(UTF8ToString(contractAddresses)), JSON.parse(UTF8ToString(accountAddresses)), @@ -134,15 +178,21 @@ mergeInto(LibraryManager.library, { const buffer = _malloc(bufferSize); stringToUTF8(balanceString, buffer, bufferSize); - client.__destroy_into_raw(); - dynCall_vi(cb, buffer); - }); + dynCall_vi(cb, buffer); + }); - client.__destroy_into_raw(); - dynCall_vi(subCb, subscription.__destroy_into_raw()); + // Store subscription in global object and return virtual pointer + let subscriptionId = nextSubscriptionId++; + toriiSubscriptions[subscriptionId] = subscription; + dynCall_vi(subCb, subscriptionId); }, OnEntityUpdated: async function (clientPtr, clauseStr, cb, subCb) { - let client = wasm_bindgen.ToriiClient.__wrap(clientPtr); + const client = toriiClients[clientPtr]; + if (!client) { + console.error('Client not found for ID:', clientPtr); + return; + } + let clause = UTF8ToString(clauseStr); const subscription = await client.onEntityUpdated(clause !== "" ? JSON.parse(clause) : undefined, (entity) => { @@ -156,21 +206,34 @@ mergeInto(LibraryManager.library, { dynCall_vi(cb, buffer); }); - client.__destroy_into_raw(); - dynCall_vi(subCb, subscription.__destroy_into_raw()); + // Store subscription in global object and return virtual pointer + let subscriptionId = nextSubscriptionId++; + toriiSubscriptions[subscriptionId] = subscription; + dynCall_vi(subCb, subscriptionId); }, UpdateEntitySubscription: async function (clientPtr, subPtr, clauseStr) { - let client = wasm_bindgen.ToriiClient.__wrap(clientPtr); - let subscription = wasm_bindgen.Subscription.__wrap(subPtr); + const client = toriiClients[clientPtr]; + const subscription = toriiSubscriptions[subPtr]; + + if (!client) { + console.error('Client not found for ID:', clientPtr); + return; + } + if (!subscription) { + console.error('Subscription not found for ID:', subPtr); + return; + } + let clause = UTF8ToString(clauseStr); - await client.updateEntitySubscription(subscription, clause !== "" ? JSON.parse(clause) : undefined); - - client.__destroy_into_raw(); - subscription.__destroy_into_raw(); }, OnEventMessageUpdated: async function (clientPtr, clauseStr, cb, subCb) { - let client = wasm_bindgen.ToriiClient.__wrap(clientPtr); + const client = toriiClients[clientPtr]; + if (!client) { + console.error('Client not found for ID:', clientPtr); + return; + } + let clause = UTF8ToString(clauseStr); const subscription = await client.onEventMessageUpdated( @@ -187,33 +250,49 @@ mergeInto(LibraryManager.library, { } ); - client.__destroy_into_raw(); - dynCall_vi(subCb, subscription.__destroy_into_raw()); + // Store subscription in global object and return virtual pointer + let subscriptionId = nextSubscriptionId++; + toriiSubscriptions[subscriptionId] = subscription; + dynCall_vi(subCb, subscriptionId); }, UpdateEventMessageSubscription: async function (clientPtr, subPtr, clauseStr) { - let client = wasm_bindgen.ToriiClient.__wrap(clientPtr); - let subscription = wasm_bindgen.Subscription.__wrap(subPtr); + const client = toriiClients[clientPtr]; + const subscription = toriiSubscriptions[subPtr]; + + if (!client) { + console.error('Client not found for ID:', clientPtr); + return; + } + if (!subscription) { + console.error('Subscription not found for ID:', subPtr); + return; + } + let clause = UTF8ToString(clauseStr); - await client.updateEventMessageSubscription(subscription, clause !== "" ? JSON.parse(clause) : undefined); - - client.__destroy_into_raw(); - subscription.__destroy_into_raw(); }, AddModelsToSync: function (clientPtr, models) { - let client = wasm_bindgen.ToriiClient.__wrap(clientPtr); + const client = toriiClients[clientPtr]; + if (!client) { + console.error('Client not found for ID:', clientPtr); + return; + } + let modelsString = UTF8ToString(models); let modelsArray = JSON.parse(modelsString); - client.__destroy_into_raw(); client.addModelsToSync(modelsArray); }, RemoveModelsToSync: function (clientPtr, models) { - let client = wasm_bindgen.ToriiClient.__wrap(clientPtr); + const client = toriiClients[clientPtr]; + if (!client) { + console.error('Client not found for ID:', clientPtr); + return; + } + let modelsString = UTF8ToString(models); let modelsArray = JSON.parse(modelsString); - client.__destroy_into_raw(); client.removeModelsToSync(modelsArray); }, OnSyncModelChange: async function ( @@ -222,7 +301,12 @@ mergeInto(LibraryManager.library, { callbackObjectName, callbackMethodName ) { - let client = wasm_bindgen.ToriiClient.__wrap(clientPtr); + const client = toriiClients[clientPtr]; + if (!client) { + console.error('Client not found for ID:', clientPtr); + return; + } + let modelsString = UTF8ToString(models); let modelsArray = JSON.parse(modelsString); @@ -232,9 +316,10 @@ mergeInto(LibraryManager.library, { UTF8ToString(callbackMethodName) ); }); - subscription.__destroy_into_raw(); - - client.__destroy_into_raw(); + + // Store subscription in global object (no need to return ID since it's not used elsewhere) + let subscriptionId = nextSubscriptionId++; + toriiSubscriptions[subscriptionId] = subscription; }, // Encode typed data with the corresponding address and return the message hash // typedData: JSON string @@ -256,7 +341,12 @@ mergeInto(LibraryManager.library, { // message: typed data JSON string // signature: string[] PublishMessage: async function (clientPtr, message, signature, cb) { - let client = wasm_bindgen.ToriiClient.__wrap(clientPtr); + const client = toriiClients[clientPtr]; + if (!client) { + console.error('Client not found for ID:', clientPtr); + return; + } + const id = await client.publishMessage({ message: UTF8ToString(message), signature: JSON.parse(UTF8ToString(signature)), @@ -265,7 +355,18 @@ mergeInto(LibraryManager.library, { const buffer = _malloc(bufferSize); stringToUTF8(id, buffer, bufferSize); - client.__destroy_into_raw(); dynCall_vi(cb, buffer); }, + // Cleanup function to dispose of a client and all its subscriptions + DisposeClient: function (clientPtr) { + if (toriiClients[clientPtr]) { + delete toriiClients[clientPtr]; + } + }, + // Cleanup function to dispose of a subscription + DisposeSubscription: function (subPtr) { + if (toriiSubscriptions[subPtr]) { + delete toriiSubscriptions[subPtr]; + } + }, }); From 242c824f7d0d9dde608677c2555c478bfb405a53 Mon Sep 17 00:00:00 2001 From: Nasr Date: Mon, 22 Sep 2025 10:16:42 -0400 Subject: [PATCH 2/4] starknet memory --- Assets/Dojo/Plugins/WebGL/starknet.jslib | 111 ++++++++++++++++++----- 1 file changed, 86 insertions(+), 25 deletions(-) diff --git a/Assets/Dojo/Plugins/WebGL/starknet.jslib b/Assets/Dojo/Plugins/WebGL/starknet.jslib index 8208fb3e..7592466b 100644 --- a/Assets/Dojo/Plugins/WebGL/starknet.jslib +++ b/Assets/Dojo/Plugins/WebGL/starknet.jslib @@ -1,76 +1,123 @@ +// Global objects to store provider and account instances +var starknetProviders = {}; +var starknetAccounts = {}; +var nextProviderId = 1; +var nextAccountId = 1; + mergeInto(LibraryManager.library, { NewProvider: function (rpcUrl) { - return new wasm_bindgen - .Provider(UTF8ToString(rpcUrl)) - .__destroy_into_raw(); + let provider = new wasm_bindgen.Provider(UTF8ToString(rpcUrl)); + + // Store provider in global object and return virtual pointer + let providerId = nextProviderId++; + starknetProviders[providerId] = provider; + return providerId; }, NewAccount: async function (providerPtr, pk, address, cb) { - const provider = wasm_bindgen.Provider.__wrap(providerPtr); + const provider = starknetProviders[providerPtr]; + if (!provider) { + console.error('Provider not found for ID:', providerPtr); + return; + } + const account = await (new wasm_bindgen.Account( provider, UTF8ToString(pk), UTF8ToString(address) )); - provider.__destroy_into_raw(); - dynCall_vi(cb, account.__destroy_into_raw()); + // Store account in global object and return virtual pointer + let accountId = nextAccountId++; + starknetAccounts[accountId] = account; + dynCall_vi(cb, accountId); }, AccountAddress: function (accountPtr) { - const account = wasm_bindgen.Account.__wrap(accountPtr); + const account = starknetAccounts[accountPtr]; + if (!account) { + console.error('Account not found for ID:', accountPtr); + return null; + } + const address = account.address(); const bufferSize = lengthBytesUTF8(address) + 1; const buffer = _malloc(bufferSize); stringToUTF8(address, buffer, bufferSize); - account.__destroy_into_raw(); return buffer; }, AccountChainId: function (accountPtr) { - const account = wasm_bindgen.Account.__wrap(accountPtr); + const account = starknetAccounts[accountPtr]; + if (!account) { + console.error('Account not found for ID:', accountPtr); + return null; + } + const chainId = account.chainId(); const bufferSize = lengthBytesUTF8(chainId) + 1; const buffer = _malloc(bufferSize); stringToUTF8(chainId, buffer, bufferSize); - account.__destroy_into_raw(); return buffer; }, AccountSetBlockId: function (accountPtr, blockId) { - const account = wasm_bindgen.Account.__wrap(accountPtr); + const account = starknetAccounts[accountPtr]; + if (!account) { + console.error('Account not found for ID:', accountPtr); + return; + } - account.__destroy_into_raw(); account.setBlockId(UTF8ToString(blockId)); }, AccountExecuteRaw: async function (accountPtr, callsStr, cb) { - const account = wasm_bindgen.Account.__wrap(accountPtr); + const account = starknetAccounts[accountPtr]; + if (!account) { + console.error('Account not found for ID:', accountPtr); + return; + } + const calls = JSON.parse(UTF8ToString(callsStr)); const txHash = await account.executeRaw(calls); const bufferSize = lengthBytesUTF8(txHash) + 1; const buffer = _malloc(bufferSize); stringToUTF8(txHash, buffer, bufferSize); - account.__destroy_into_raw(); dynCall_vi(cb, buffer); }, AccountDeployBurner: async function (accountPtr, privateKey, cb) { - const account = wasm_bindgen.Account.__wrap(accountPtr); + const account = starknetAccounts[accountPtr]; + if (!account) { + console.error('Account not found for ID:', accountPtr); + return; + } + const burner = await account.deployBurner(UTF8ToString(privateKey)); - account.__destroy_into_raw(); - dynCall_vi(cb, burner.__destroy_into_raw()); + // Store burner account in global object and return virtual pointer + let burnerId = nextAccountId++; + starknetAccounts[burnerId] = burner; + dynCall_vi(cb, burnerId); }, AccountNonce: async function (accountPtr, cb) { - const account = wasm_bindgen.Account.__wrap(accountPtr); + const account = starknetAccounts[accountPtr]; + if (!account) { + console.error('Account not found for ID:', accountPtr); + return; + } + const nonce = await account.nonce(); const bufferSize = lengthBytesUTF8(nonce) + 1; const buffer = _malloc(bufferSize); stringToUTF8(nonce, buffer, bufferSize); - account.__destroy_into_raw(); dynCall_vi(cb, buffer); -}, + }, Call: async function (providerPtr, callStr, blockIdStr, cb) { - const provider = wasm_bindgen.Provider.__wrap(providerPtr); + const provider = starknetProviders[providerPtr]; + if (!provider) { + console.error('Provider not found for ID:', providerPtr); + return; + } + const call = JSON.parse(UTF8ToString(callStr)); const blockId = JSON.parse(UTF8ToString(blockIdStr)); const result = await provider.call(call, blockId); @@ -78,14 +125,17 @@ mergeInto(LibraryManager.library, { const buffer = _malloc(bufferSize); stringToUTF8(result, buffer, bufferSize); - provider.__destroy_into_raw(); dynCall_vi(cb, buffer); }, WaitForTransaction: async function (providerPtr, txHash, cb) { - const provider = wasm_bindgen.Provider.__wrap(providerPtr); + const provider = starknetProviders[providerPtr]; + if (!provider) { + console.error('Provider not found for ID:', providerPtr); + return; + } + const confirmed = await provider.waitForTransaction(UTF8ToString(txHash)); - provider.__destroy_into_raw(); dynCall_vi(cb, confirmed); }, RandomSigningKey: function () { @@ -155,5 +205,16 @@ mergeInto(LibraryManager.library, { stringToUTF8(hash, buffer, bufferSize); return buffer; }, - + // Cleanup function to dispose of a provider + DisposeProvider: function (providerPtr) { + if (starknetProviders[providerPtr]) { + delete starknetProviders[providerPtr]; + } + }, + // Cleanup function to dispose of an account + DisposeAccount: function (accountPtr) { + if (starknetAccounts[accountPtr]) { + delete starknetAccounts[accountPtr]; + } + }, }); From a3fb83870fc43b61c92286c67de1a08b74975489 Mon Sep 17 00:00:00 2001 From: Nasr Date: Mon, 22 Sep 2025 10:49:04 -0400 Subject: [PATCH 3/4] fix --- Assets/Dojo/Plugins/WebGL/starknet.jslib | 49 +++++++-------- Assets/Dojo/Plugins/WebGL/torii_c.jslib | 79 ++++++++++++------------ 2 files changed, 63 insertions(+), 65 deletions(-) diff --git a/Assets/Dojo/Plugins/WebGL/starknet.jslib b/Assets/Dojo/Plugins/WebGL/starknet.jslib index 7592466b..f29df463 100644 --- a/Assets/Dojo/Plugins/WebGL/starknet.jslib +++ b/Assets/Dojo/Plugins/WebGL/starknet.jslib @@ -1,20 +1,19 @@ -// Global objects to store provider and account instances -var starknetProviders = {}; -var starknetAccounts = {}; -var nextProviderId = 1; -var nextAccountId = 1; - mergeInto(LibraryManager.library, { + // Global objects to store provider and account instances + starknetProviders: {}, + starknetAccounts: {}, + nextProviderId: 1, + nextAccountId: 1, NewProvider: function (rpcUrl) { let provider = new wasm_bindgen.Provider(UTF8ToString(rpcUrl)); // Store provider in global object and return virtual pointer - let providerId = nextProviderId++; - starknetProviders[providerId] = provider; + let providerId = this.nextProviderId++; + this.starknetProviders[providerId] = provider; return providerId; }, NewAccount: async function (providerPtr, pk, address, cb) { - const provider = starknetProviders[providerPtr]; + const provider = this.starknetProviders[providerPtr]; if (!provider) { console.error('Provider not found for ID:', providerPtr); return; @@ -27,12 +26,12 @@ mergeInto(LibraryManager.library, { )); // Store account in global object and return virtual pointer - let accountId = nextAccountId++; - starknetAccounts[accountId] = account; + let accountId = this.nextAccountId++; + this.starknetAccounts[accountId] = account; dynCall_vi(cb, accountId); }, AccountAddress: function (accountPtr) { - const account = starknetAccounts[accountPtr]; + const account = this.starknetAccounts[accountPtr]; if (!account) { console.error('Account not found for ID:', accountPtr); return null; @@ -46,7 +45,7 @@ mergeInto(LibraryManager.library, { return buffer; }, AccountChainId: function (accountPtr) { - const account = starknetAccounts[accountPtr]; + const account = this.starknetAccounts[accountPtr]; if (!account) { console.error('Account not found for ID:', accountPtr); return null; @@ -60,7 +59,7 @@ mergeInto(LibraryManager.library, { return buffer; }, AccountSetBlockId: function (accountPtr, blockId) { - const account = starknetAccounts[accountPtr]; + const account = this.starknetAccounts[accountPtr]; if (!account) { console.error('Account not found for ID:', accountPtr); return; @@ -69,7 +68,7 @@ mergeInto(LibraryManager.library, { account.setBlockId(UTF8ToString(blockId)); }, AccountExecuteRaw: async function (accountPtr, callsStr, cb) { - const account = starknetAccounts[accountPtr]; + const account = this.starknetAccounts[accountPtr]; if (!account) { console.error('Account not found for ID:', accountPtr); return; @@ -84,7 +83,7 @@ mergeInto(LibraryManager.library, { dynCall_vi(cb, buffer); }, AccountDeployBurner: async function (accountPtr, privateKey, cb) { - const account = starknetAccounts[accountPtr]; + const account = this.starknetAccounts[accountPtr]; if (!account) { console.error('Account not found for ID:', accountPtr); return; @@ -93,12 +92,12 @@ mergeInto(LibraryManager.library, { const burner = await account.deployBurner(UTF8ToString(privateKey)); // Store burner account in global object and return virtual pointer - let burnerId = nextAccountId++; - starknetAccounts[burnerId] = burner; + let burnerId = this.nextAccountId++; + this.starknetAccounts[burnerId] = burner; dynCall_vi(cb, burnerId); }, AccountNonce: async function (accountPtr, cb) { - const account = starknetAccounts[accountPtr]; + const account = this.starknetAccounts[accountPtr]; if (!account) { console.error('Account not found for ID:', accountPtr); return; @@ -112,7 +111,7 @@ mergeInto(LibraryManager.library, { dynCall_vi(cb, buffer); }, Call: async function (providerPtr, callStr, blockIdStr, cb) { - const provider = starknetProviders[providerPtr]; + const provider = this.starknetProviders[providerPtr]; if (!provider) { console.error('Provider not found for ID:', providerPtr); return; @@ -128,7 +127,7 @@ mergeInto(LibraryManager.library, { dynCall_vi(cb, buffer); }, WaitForTransaction: async function (providerPtr, txHash, cb) { - const provider = starknetProviders[providerPtr]; + const provider = this.starknetProviders[providerPtr]; if (!provider) { console.error('Provider not found for ID:', providerPtr); return; @@ -207,14 +206,14 @@ mergeInto(LibraryManager.library, { }, // Cleanup function to dispose of a provider DisposeProvider: function (providerPtr) { - if (starknetProviders[providerPtr]) { - delete starknetProviders[providerPtr]; + if (this.starknetProviders[providerPtr]) { + delete this.starknetProviders[providerPtr]; } }, // Cleanup function to dispose of an account DisposeAccount: function (accountPtr) { - if (starknetAccounts[accountPtr]) { - delete starknetAccounts[accountPtr]; + if (this.starknetAccounts[accountPtr]) { + delete this.starknetAccounts[accountPtr]; } }, }); diff --git a/Assets/Dojo/Plugins/WebGL/torii_c.jslib b/Assets/Dojo/Plugins/WebGL/torii_c.jslib index 92508bb2..71aba3b6 100644 --- a/Assets/Dojo/Plugins/WebGL/torii_c.jslib +++ b/Assets/Dojo/Plugins/WebGL/torii_c.jslib @@ -1,10 +1,9 @@ -// Global objects to store client instances and subscriptions -var toriiClients = {}; -var toriiSubscriptions = {}; -var nextClientId = 1; -var nextSubscriptionId = 1; - mergeInto(LibraryManager.library, { + // Global objects to store client instances and subscriptions + toriiClients: {}, + toriiSubscriptions: {}, + nextClientId: 1, + nextSubscriptionId: 1, // Creates a new client and returns the pointer to it CreateClient: async function ( toriiUrl, @@ -17,13 +16,13 @@ mergeInto(LibraryManager.library, { })); // Store client in global object and return virtual pointer - let clientId = nextClientId++; - toriiClients[clientId] = client; + let clientId = this.nextClientId++; + this.toriiClients[clientId] = client; dynCall_vi(cb, clientId); }, // Returns a page of all controllers GetControllers: async function (clientPtr, queryString, cb) { - const client = toriiClients[clientPtr]; + const client = this.toriiClients[clientPtr]; if (!client) { console.error('Client not found for ID:', clientPtr); return; @@ -41,7 +40,7 @@ mergeInto(LibraryManager.library, { }, // Returns a page of all tokens GetTokens: async function (clientPtr, queryString, cb) { - const client = toriiClients[clientPtr]; + const client = this.toriiClients[clientPtr]; if (!client) { console.error('Client not found for ID:', clientPtr); return; @@ -59,7 +58,7 @@ mergeInto(LibraryManager.library, { }, // Returns a page of all token balances GetTokenBalances: async function (clientPtr, queryString, cb) { - const client = toriiClients[clientPtr]; + const client = this.toriiClients[clientPtr]; if (!client) { console.error('Client not found for ID:', clientPtr); return; @@ -77,7 +76,7 @@ mergeInto(LibraryManager.library, { }, // Returns a dictionary of all of the entities GetEntities: async function (clientPtr, queryString, cb) { - const client = toriiClients[clientPtr]; + const client = this.toriiClients[clientPtr]; if (!client) { console.error('Client not found for ID:', clientPtr); return; @@ -97,7 +96,7 @@ mergeInto(LibraryManager.library, { }, // Returns a dictionary of all of the eventmessages GetEventMessages: async function (clientPtr, queryString, cb) { - const client = toriiClients[clientPtr]; + const client = this.toriiClients[clientPtr]; if (!client) { console.error('Client not found for ID:', clientPtr); return; @@ -117,7 +116,7 @@ mergeInto(LibraryManager.library, { }, // Get the value of a model for a specific set of keys GetModelValue: async function (clientPtr, model, keys, cb) { - const client = toriiClients[clientPtr]; + const client = this.toriiClients[clientPtr]; if (!client) { console.error('Client not found for ID:', clientPtr); return; @@ -138,7 +137,7 @@ mergeInto(LibraryManager.library, { dynCall_vi(cb, buffer); }, OnTokenUpdated: async function (clientPtr, contractAddresses, tokenIds, cb, subCb) { - const client = toriiClients[clientPtr]; + const client = this.toriiClients[clientPtr]; if (!client) { console.error('Client not found for ID:', clientPtr); return; @@ -157,12 +156,12 @@ mergeInto(LibraryManager.library, { }); // Store subscription in global object and return virtual pointer - let subscriptionId = nextSubscriptionId++; - toriiSubscriptions[subscriptionId] = subscription; + let subscriptionId = this.nextSubscriptionId++; + this.toriiSubscriptions[subscriptionId] = subscription; dynCall_vi(subCb, subscriptionId); }, OnTokenBalanceUpdated: async function (clientPtr, contractAddresses, accountAddresses, tokenIds, cb, subCb) { - const client = toriiClients[clientPtr]; + const client = this.toriiClients[clientPtr]; if (!client) { console.error('Client not found for ID:', clientPtr); return; @@ -182,12 +181,12 @@ mergeInto(LibraryManager.library, { }); // Store subscription in global object and return virtual pointer - let subscriptionId = nextSubscriptionId++; - toriiSubscriptions[subscriptionId] = subscription; + let subscriptionId = this.nextSubscriptionId++; + this.toriiSubscriptions[subscriptionId] = subscription; dynCall_vi(subCb, subscriptionId); }, OnEntityUpdated: async function (clientPtr, clauseStr, cb, subCb) { - const client = toriiClients[clientPtr]; + const client = this.toriiClients[clientPtr]; if (!client) { console.error('Client not found for ID:', clientPtr); return; @@ -207,13 +206,13 @@ mergeInto(LibraryManager.library, { }); // Store subscription in global object and return virtual pointer - let subscriptionId = nextSubscriptionId++; - toriiSubscriptions[subscriptionId] = subscription; + let subscriptionId = this.nextSubscriptionId++; + this.toriiSubscriptions[subscriptionId] = subscription; dynCall_vi(subCb, subscriptionId); }, UpdateEntitySubscription: async function (clientPtr, subPtr, clauseStr) { - const client = toriiClients[clientPtr]; - const subscription = toriiSubscriptions[subPtr]; + const client = this.toriiClients[clientPtr]; + const subscription = this.toriiSubscriptions[subPtr]; if (!client) { console.error('Client not found for ID:', clientPtr); @@ -228,7 +227,7 @@ mergeInto(LibraryManager.library, { await client.updateEntitySubscription(subscription, clause !== "" ? JSON.parse(clause) : undefined); }, OnEventMessageUpdated: async function (clientPtr, clauseStr, cb, subCb) { - const client = toriiClients[clientPtr]; + const client = this.toriiClients[clientPtr]; if (!client) { console.error('Client not found for ID:', clientPtr); return; @@ -251,13 +250,13 @@ mergeInto(LibraryManager.library, { ); // Store subscription in global object and return virtual pointer - let subscriptionId = nextSubscriptionId++; - toriiSubscriptions[subscriptionId] = subscription; + let subscriptionId = this.nextSubscriptionId++; + this.toriiSubscriptions[subscriptionId] = subscription; dynCall_vi(subCb, subscriptionId); }, UpdateEventMessageSubscription: async function (clientPtr, subPtr, clauseStr) { - const client = toriiClients[clientPtr]; - const subscription = toriiSubscriptions[subPtr]; + const client = this.toriiClients[clientPtr]; + const subscription = this.toriiSubscriptions[subPtr]; if (!client) { console.error('Client not found for ID:', clientPtr); @@ -272,7 +271,7 @@ mergeInto(LibraryManager.library, { await client.updateEventMessageSubscription(subscription, clause !== "" ? JSON.parse(clause) : undefined); }, AddModelsToSync: function (clientPtr, models) { - const client = toriiClients[clientPtr]; + const client = this.toriiClients[clientPtr]; if (!client) { console.error('Client not found for ID:', clientPtr); return; @@ -284,7 +283,7 @@ mergeInto(LibraryManager.library, { client.addModelsToSync(modelsArray); }, RemoveModelsToSync: function (clientPtr, models) { - const client = toriiClients[clientPtr]; + const client = this.toriiClients[clientPtr]; if (!client) { console.error('Client not found for ID:', clientPtr); return; @@ -301,7 +300,7 @@ mergeInto(LibraryManager.library, { callbackObjectName, callbackMethodName ) { - const client = toriiClients[clientPtr]; + const client = this.toriiClients[clientPtr]; if (!client) { console.error('Client not found for ID:', clientPtr); return; @@ -318,8 +317,8 @@ mergeInto(LibraryManager.library, { }); // Store subscription in global object (no need to return ID since it's not used elsewhere) - let subscriptionId = nextSubscriptionId++; - toriiSubscriptions[subscriptionId] = subscription; + let subscriptionId = this.nextSubscriptionId++; + this.toriiSubscriptions[subscriptionId] = subscription; }, // Encode typed data with the corresponding address and return the message hash // typedData: JSON string @@ -341,7 +340,7 @@ mergeInto(LibraryManager.library, { // message: typed data JSON string // signature: string[] PublishMessage: async function (clientPtr, message, signature, cb) { - const client = toriiClients[clientPtr]; + const client = this.toriiClients[clientPtr]; if (!client) { console.error('Client not found for ID:', clientPtr); return; @@ -359,14 +358,14 @@ mergeInto(LibraryManager.library, { }, // Cleanup function to dispose of a client and all its subscriptions DisposeClient: function (clientPtr) { - if (toriiClients[clientPtr]) { - delete toriiClients[clientPtr]; + if (this.toriiClients[clientPtr]) { + delete this.toriiClients[clientPtr]; } }, // Cleanup function to dispose of a subscription DisposeSubscription: function (subPtr) { - if (toriiSubscriptions[subPtr]) { - delete toriiSubscriptions[subPtr]; + if (this.toriiSubscriptions[subPtr]) { + delete this.toriiSubscriptions[subPtr]; } }, }); From ae0c7499b6ea13acfdec9d07efaa5e3844e3d3f1 Mon Sep 17 00:00:00 2001 From: Nasr Date: Mon, 22 Sep 2025 11:28:39 -0400 Subject: [PATCH 4/4] lazy initialize --- Assets/Dojo/Plugins/WebGL/starknet.jslib | 36 +++++++------- Assets/Dojo/Plugins/WebGL/torii_c.jslib | 63 ++++++++++++------------ 2 files changed, 49 insertions(+), 50 deletions(-) diff --git a/Assets/Dojo/Plugins/WebGL/starknet.jslib b/Assets/Dojo/Plugins/WebGL/starknet.jslib index f29df463..6da46bba 100644 --- a/Assets/Dojo/Plugins/WebGL/starknet.jslib +++ b/Assets/Dojo/Plugins/WebGL/starknet.jslib @@ -1,19 +1,15 @@ mergeInto(LibraryManager.library, { - // Global objects to store provider and account instances - starknetProviders: {}, - starknetAccounts: {}, - nextProviderId: 1, - nextAccountId: 1, NewProvider: function (rpcUrl) { let provider = new wasm_bindgen.Provider(UTF8ToString(rpcUrl)); // Store provider in global object and return virtual pointer - let providerId = this.nextProviderId++; + let providerId = this.nextProviderId ? this.nextProviderId++ : (this.nextProviderId = 2, 1); + this.starknetProviders = this.starknetProviders || {}; this.starknetProviders[providerId] = provider; return providerId; }, NewAccount: async function (providerPtr, pk, address, cb) { - const provider = this.starknetProviders[providerPtr]; + const provider = (this.starknetProviders || {})[providerPtr]; if (!provider) { console.error('Provider not found for ID:', providerPtr); return; @@ -26,12 +22,13 @@ mergeInto(LibraryManager.library, { )); // Store account in global object and return virtual pointer - let accountId = this.nextAccountId++; + let accountId = this.nextAccountId ? this.nextAccountId++ : (this.nextAccountId = 2, 1); + this.starknetAccounts = this.starknetAccounts || {}; this.starknetAccounts[accountId] = account; dynCall_vi(cb, accountId); }, AccountAddress: function (accountPtr) { - const account = this.starknetAccounts[accountPtr]; + const account = (this.starknetAccounts || {})[accountPtr]; if (!account) { console.error('Account not found for ID:', accountPtr); return null; @@ -45,7 +42,7 @@ mergeInto(LibraryManager.library, { return buffer; }, AccountChainId: function (accountPtr) { - const account = this.starknetAccounts[accountPtr]; + const account = (this.starknetAccounts || {})[accountPtr]; if (!account) { console.error('Account not found for ID:', accountPtr); return null; @@ -59,7 +56,7 @@ mergeInto(LibraryManager.library, { return buffer; }, AccountSetBlockId: function (accountPtr, blockId) { - const account = this.starknetAccounts[accountPtr]; + const account = (this.starknetAccounts || {})[accountPtr]; if (!account) { console.error('Account not found for ID:', accountPtr); return; @@ -68,7 +65,7 @@ mergeInto(LibraryManager.library, { account.setBlockId(UTF8ToString(blockId)); }, AccountExecuteRaw: async function (accountPtr, callsStr, cb) { - const account = this.starknetAccounts[accountPtr]; + const account = (this.starknetAccounts || {})[accountPtr]; if (!account) { console.error('Account not found for ID:', accountPtr); return; @@ -83,7 +80,7 @@ mergeInto(LibraryManager.library, { dynCall_vi(cb, buffer); }, AccountDeployBurner: async function (accountPtr, privateKey, cb) { - const account = this.starknetAccounts[accountPtr]; + const account = (this.starknetAccounts || {})[accountPtr]; if (!account) { console.error('Account not found for ID:', accountPtr); return; @@ -92,12 +89,13 @@ mergeInto(LibraryManager.library, { const burner = await account.deployBurner(UTF8ToString(privateKey)); // Store burner account in global object and return virtual pointer - let burnerId = this.nextAccountId++; + let burnerId = this.nextAccountId ? this.nextAccountId++ : (this.nextAccountId = 2, 1); + this.starknetAccounts = this.starknetAccounts || {}; this.starknetAccounts[burnerId] = burner; dynCall_vi(cb, burnerId); }, AccountNonce: async function (accountPtr, cb) { - const account = this.starknetAccounts[accountPtr]; + const account = (this.starknetAccounts || {})[accountPtr]; if (!account) { console.error('Account not found for ID:', accountPtr); return; @@ -111,7 +109,7 @@ mergeInto(LibraryManager.library, { dynCall_vi(cb, buffer); }, Call: async function (providerPtr, callStr, blockIdStr, cb) { - const provider = this.starknetProviders[providerPtr]; + const provider = (this.starknetProviders || {})[providerPtr]; if (!provider) { console.error('Provider not found for ID:', providerPtr); return; @@ -127,7 +125,7 @@ mergeInto(LibraryManager.library, { dynCall_vi(cb, buffer); }, WaitForTransaction: async function (providerPtr, txHash, cb) { - const provider = this.starknetProviders[providerPtr]; + const provider = (this.starknetProviders || {})[providerPtr]; if (!provider) { console.error('Provider not found for ID:', providerPtr); return; @@ -206,13 +204,13 @@ mergeInto(LibraryManager.library, { }, // Cleanup function to dispose of a provider DisposeProvider: function (providerPtr) { - if (this.starknetProviders[providerPtr]) { + if (this.starknetProviders && this.starknetProviders[providerPtr]) { delete this.starknetProviders[providerPtr]; } }, // Cleanup function to dispose of an account DisposeAccount: function (accountPtr) { - if (this.starknetAccounts[accountPtr]) { + if (this.starknetAccounts && this.starknetAccounts[accountPtr]) { delete this.starknetAccounts[accountPtr]; } }, diff --git a/Assets/Dojo/Plugins/WebGL/torii_c.jslib b/Assets/Dojo/Plugins/WebGL/torii_c.jslib index 71aba3b6..cbdef2f0 100644 --- a/Assets/Dojo/Plugins/WebGL/torii_c.jslib +++ b/Assets/Dojo/Plugins/WebGL/torii_c.jslib @@ -1,9 +1,4 @@ mergeInto(LibraryManager.library, { - // Global objects to store client instances and subscriptions - toriiClients: {}, - toriiSubscriptions: {}, - nextClientId: 1, - nextSubscriptionId: 1, // Creates a new client and returns the pointer to it CreateClient: async function ( toriiUrl, @@ -16,13 +11,14 @@ mergeInto(LibraryManager.library, { })); // Store client in global object and return virtual pointer - let clientId = this.nextClientId++; + let clientId = this.nextClientId ? this.nextClientId++ : (this.nextClientId = 2, 1); + this.toriiClients = this.toriiClients || {}; this.toriiClients[clientId] = client; dynCall_vi(cb, clientId); }, // Returns a page of all controllers GetControllers: async function (clientPtr, queryString, cb) { - const client = this.toriiClients[clientPtr]; + const client = (this.toriiClients || {})[clientPtr]; if (!client) { console.error('Client not found for ID:', clientPtr); return; @@ -40,7 +36,7 @@ mergeInto(LibraryManager.library, { }, // Returns a page of all tokens GetTokens: async function (clientPtr, queryString, cb) { - const client = this.toriiClients[clientPtr]; + const client = (this.toriiClients || {})[clientPtr]; if (!client) { console.error('Client not found for ID:', clientPtr); return; @@ -58,7 +54,7 @@ mergeInto(LibraryManager.library, { }, // Returns a page of all token balances GetTokenBalances: async function (clientPtr, queryString, cb) { - const client = this.toriiClients[clientPtr]; + const client = (this.toriiClients || {})[clientPtr]; if (!client) { console.error('Client not found for ID:', clientPtr); return; @@ -76,7 +72,7 @@ mergeInto(LibraryManager.library, { }, // Returns a dictionary of all of the entities GetEntities: async function (clientPtr, queryString, cb) { - const client = this.toriiClients[clientPtr]; + const client = (this.toriiClients || {})[clientPtr]; if (!client) { console.error('Client not found for ID:', clientPtr); return; @@ -96,7 +92,7 @@ mergeInto(LibraryManager.library, { }, // Returns a dictionary of all of the eventmessages GetEventMessages: async function (clientPtr, queryString, cb) { - const client = this.toriiClients[clientPtr]; + const client = (this.toriiClients || {})[clientPtr]; if (!client) { console.error('Client not found for ID:', clientPtr); return; @@ -116,7 +112,7 @@ mergeInto(LibraryManager.library, { }, // Get the value of a model for a specific set of keys GetModelValue: async function (clientPtr, model, keys, cb) { - const client = this.toriiClients[clientPtr]; + const client = (this.toriiClients || {})[clientPtr]; if (!client) { console.error('Client not found for ID:', clientPtr); return; @@ -137,7 +133,7 @@ mergeInto(LibraryManager.library, { dynCall_vi(cb, buffer); }, OnTokenUpdated: async function (clientPtr, contractAddresses, tokenIds, cb, subCb) { - const client = this.toriiClients[clientPtr]; + const client = (this.toriiClients || {})[clientPtr]; if (!client) { console.error('Client not found for ID:', clientPtr); return; @@ -156,12 +152,13 @@ mergeInto(LibraryManager.library, { }); // Store subscription in global object and return virtual pointer - let subscriptionId = this.nextSubscriptionId++; + let subscriptionId = this.nextSubscriptionId ? this.nextSubscriptionId++ : (this.nextSubscriptionId = 2, 1); + this.toriiSubscriptions = this.toriiSubscriptions || {}; this.toriiSubscriptions[subscriptionId] = subscription; dynCall_vi(subCb, subscriptionId); }, OnTokenBalanceUpdated: async function (clientPtr, contractAddresses, accountAddresses, tokenIds, cb, subCb) { - const client = this.toriiClients[clientPtr]; + const client = (this.toriiClients || {})[clientPtr]; if (!client) { console.error('Client not found for ID:', clientPtr); return; @@ -181,12 +178,13 @@ mergeInto(LibraryManager.library, { }); // Store subscription in global object and return virtual pointer - let subscriptionId = this.nextSubscriptionId++; + let subscriptionId = this.nextSubscriptionId ? this.nextSubscriptionId++ : (this.nextSubscriptionId = 2, 1); + this.toriiSubscriptions = this.toriiSubscriptions || {}; this.toriiSubscriptions[subscriptionId] = subscription; dynCall_vi(subCb, subscriptionId); }, OnEntityUpdated: async function (clientPtr, clauseStr, cb, subCb) { - const client = this.toriiClients[clientPtr]; + const client = (this.toriiClients || {})[clientPtr]; if (!client) { console.error('Client not found for ID:', clientPtr); return; @@ -206,13 +204,14 @@ mergeInto(LibraryManager.library, { }); // Store subscription in global object and return virtual pointer - let subscriptionId = this.nextSubscriptionId++; + let subscriptionId = this.nextSubscriptionId ? this.nextSubscriptionId++ : (this.nextSubscriptionId = 2, 1); + this.toriiSubscriptions = this.toriiSubscriptions || {}; this.toriiSubscriptions[subscriptionId] = subscription; dynCall_vi(subCb, subscriptionId); }, UpdateEntitySubscription: async function (clientPtr, subPtr, clauseStr) { - const client = this.toriiClients[clientPtr]; - const subscription = this.toriiSubscriptions[subPtr]; + const client = (this.toriiClients || {})[clientPtr]; + const subscription = (this.toriiSubscriptions || {})[subPtr]; if (!client) { console.error('Client not found for ID:', clientPtr); @@ -227,7 +226,7 @@ mergeInto(LibraryManager.library, { await client.updateEntitySubscription(subscription, clause !== "" ? JSON.parse(clause) : undefined); }, OnEventMessageUpdated: async function (clientPtr, clauseStr, cb, subCb) { - const client = this.toriiClients[clientPtr]; + const client = (this.toriiClients || {})[clientPtr]; if (!client) { console.error('Client not found for ID:', clientPtr); return; @@ -250,13 +249,14 @@ mergeInto(LibraryManager.library, { ); // Store subscription in global object and return virtual pointer - let subscriptionId = this.nextSubscriptionId++; + let subscriptionId = this.nextSubscriptionId ? this.nextSubscriptionId++ : (this.nextSubscriptionId = 2, 1); + this.toriiSubscriptions = this.toriiSubscriptions || {}; this.toriiSubscriptions[subscriptionId] = subscription; dynCall_vi(subCb, subscriptionId); }, UpdateEventMessageSubscription: async function (clientPtr, subPtr, clauseStr) { - const client = this.toriiClients[clientPtr]; - const subscription = this.toriiSubscriptions[subPtr]; + const client = (this.toriiClients || {})[clientPtr]; + const subscription = (this.toriiSubscriptions || {})[subPtr]; if (!client) { console.error('Client not found for ID:', clientPtr); @@ -271,7 +271,7 @@ mergeInto(LibraryManager.library, { await client.updateEventMessageSubscription(subscription, clause !== "" ? JSON.parse(clause) : undefined); }, AddModelsToSync: function (clientPtr, models) { - const client = this.toriiClients[clientPtr]; + const client = (this.toriiClients || {})[clientPtr]; if (!client) { console.error('Client not found for ID:', clientPtr); return; @@ -283,7 +283,7 @@ mergeInto(LibraryManager.library, { client.addModelsToSync(modelsArray); }, RemoveModelsToSync: function (clientPtr, models) { - const client = this.toriiClients[clientPtr]; + const client = (this.toriiClients || {})[clientPtr]; if (!client) { console.error('Client not found for ID:', clientPtr); return; @@ -300,7 +300,7 @@ mergeInto(LibraryManager.library, { callbackObjectName, callbackMethodName ) { - const client = this.toriiClients[clientPtr]; + const client = (this.toriiClients || {})[clientPtr]; if (!client) { console.error('Client not found for ID:', clientPtr); return; @@ -317,7 +317,8 @@ mergeInto(LibraryManager.library, { }); // Store subscription in global object (no need to return ID since it's not used elsewhere) - let subscriptionId = this.nextSubscriptionId++; + let subscriptionId = this.nextSubscriptionId ? this.nextSubscriptionId++ : (this.nextSubscriptionId = 2, 1); + this.toriiSubscriptions = this.toriiSubscriptions || {}; this.toriiSubscriptions[subscriptionId] = subscription; }, // Encode typed data with the corresponding address and return the message hash @@ -340,7 +341,7 @@ mergeInto(LibraryManager.library, { // message: typed data JSON string // signature: string[] PublishMessage: async function (clientPtr, message, signature, cb) { - const client = this.toriiClients[clientPtr]; + const client = (this.toriiClients || {})[clientPtr]; if (!client) { console.error('Client not found for ID:', clientPtr); return; @@ -358,13 +359,13 @@ mergeInto(LibraryManager.library, { }, // Cleanup function to dispose of a client and all its subscriptions DisposeClient: function (clientPtr) { - if (this.toriiClients[clientPtr]) { + if (this.toriiClients && this.toriiClients[clientPtr]) { delete this.toriiClients[clientPtr]; } }, // Cleanup function to dispose of a subscription DisposeSubscription: function (subPtr) { - if (this.toriiSubscriptions[subPtr]) { + if (this.toriiSubscriptions && this.toriiSubscriptions[subPtr]) { delete this.toriiSubscriptions[subPtr]; } },