@bytecodealliance/jco 1.17.4 → 1.17.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2051,10 +2051,52 @@ function _lowerImportBackwardsCompat(args) {
2051
2051
  importFn,
2052
2052
  } = args;
2053
2053
 
2054
- const { taskID } = _getGlobalCurrentTaskMeta(componentIdx);
2054
+ let meta = _getGlobalCurrentTaskMeta(componentIdx);
2055
+ let createdTask;
2056
+
2057
+ // Some components depend on initialization logic (i.e. `_initialize` or some such
2058
+ // core wasm export) that is embedded in the component, but is not executed or wizer'd
2059
+ // away before the transpiled component is attempted to be used.
2060
+ //
2061
+ // These components execut their initialization logic *when they are imported* in the
2062
+ // transpiled context -- so we may get a call to an export that is lowered without going
2063
+ // through `CallWasm` or `CallInterface`.
2064
+ //
2065
+ if (!meta) {
2066
+ if (funcTypeIsAsync || (isAsync && !isManualAsync)) {
2067
+ throw new Error('p3 async wasm exports cannot use backwards compat auto-task init');
2068
+ }
2069
+
2070
+ const [newTask, newTaskID] = createNewCurrentTask({
2071
+ componentIdx,
2072
+ isAsync,
2073
+ isManualAsync,
2074
+ callingWasmExport: false,
2075
+ });
2076
+ createdTask = newTask;
2077
+
2078
+ // Since we're managing the task creation ourselves we must clear ourselves
2079
+ createdTask.registerOnResolveHandler(() => {
2080
+ _clearCurrentTask({
2081
+ taskID: task.id(),
2082
+ componentIdx: task.componentIdx(),
2083
+ });
2084
+ });
2085
+
2086
+ _setGlobalCurrentTaskMeta({
2087
+ componentIdx,
2088
+ taskID: newTaskID,
2089
+ });
2090
+
2091
+ meta = _getGlobalCurrentTaskMeta(componentIdx);
2092
+ }
2093
+
2094
+ const { taskID } = meta;
2055
2095
 
2056
2096
  const taskMeta = getCurrentTask(componentIdx, taskID);
2057
- if (!taskMeta) { throw new Error('invalid/missing async task meta'); }
2097
+ if (!taskMeta) {
2098
+ throw new Error('invalid/missing async task meta');
2099
+ }
2058
2100
 
2059
2101
  const task = taskMeta.task;
2060
2102
  if (!task) { throw new Error('invalid/missing async task'); }
@@ -2098,13 +2140,20 @@ function _lowerImportBackwardsCompat(args) {
2098
2140
  // TODO(breaking): remove once we get rid of manual async import specification,
2099
2141
  // as func types cannot be detected in that case only (and we don't need that w/ p3)
2100
2142
  if (!isManualAsync && !isAsync && !funcTypeIsAsync) {
2143
+ if (createdTask) { createdTask.enterSync(); }
2144
+
2101
2145
  const res = importFn(...params);
2146
+
2102
2147
  // TODO(breaking): remove once we get rid of manual async import specification,
2103
2148
  // as func types cannot be detected in that case only (and we don't need that w/ p3)
2104
2149
  if (!funcTypeIsAsync && !subtask.isReturned()) {
2105
2150
  throw new Error('post-execution subtasks must either be async or returned');
2106
2151
  }
2107
- return subtask.getResult();
2152
+
2153
+ const syncRes = subtask.getResult();
2154
+ if (createdTask) { createdTask.resolve([syncRes]); }
2155
+
2156
+ return syncRes;
2108
2157
  }
2109
2158
 
2110
2159
  // Sync-lowered async functions requires async behavior because the callee *can* block,
@@ -2173,10 +2222,16 @@ function _lowerImportBackwardsCompat(args) {
2173
2222
  queueMicrotask(async () => {
2174
2223
  try {
2175
2224
  _debugLog('[_lowerImportBackwardsCompat()] calling lowered import', { importFn, params });
2176
- const res = await importFn(...params);
2225
+ if (createdTask) { await createdTask.enter(); }
2226
+
2227
+ const asyncRes = await importFn(...params);
2177
2228
  if (requiresManualAsyncResult) {
2178
2229
  manualAsyncResult.resolve(subtask.getResult());
2179
2230
  }
2231
+
2232
+ if (createdTask) { createdTask.resolve([asyncRes]); }
2233
+
2234
+
2180
2235
  } catch (err) {
2181
2236
  _debugLog("[_lowerImportBackwardsCompat()] import fn error:", err);
2182
2237
  if (requiresManualAsyncResult) {
package/obj/wasm-tools.js CHANGED
@@ -2051,10 +2051,52 @@ function _lowerImportBackwardsCompat(args) {
2051
2051
  importFn,
2052
2052
  } = args;
2053
2053
 
2054
- const { taskID } = _getGlobalCurrentTaskMeta(componentIdx);
2054
+ let meta = _getGlobalCurrentTaskMeta(componentIdx);
2055
+ let createdTask;
2056
+
2057
+ // Some components depend on initialization logic (i.e. `_initialize` or some such
2058
+ // core wasm export) that is embedded in the component, but is not executed or wizer'd
2059
+ // away before the transpiled component is attempted to be used.
2060
+ //
2061
+ // These components execut their initialization logic *when they are imported* in the
2062
+ // transpiled context -- so we may get a call to an export that is lowered without going
2063
+ // through `CallWasm` or `CallInterface`.
2064
+ //
2065
+ if (!meta) {
2066
+ if (funcTypeIsAsync || (isAsync && !isManualAsync)) {
2067
+ throw new Error('p3 async wasm exports cannot use backwards compat auto-task init');
2068
+ }
2069
+
2070
+ const [newTask, newTaskID] = createNewCurrentTask({
2071
+ componentIdx,
2072
+ isAsync,
2073
+ isManualAsync,
2074
+ callingWasmExport: false,
2075
+ });
2076
+ createdTask = newTask;
2077
+
2078
+ // Since we're managing the task creation ourselves we must clear ourselves
2079
+ createdTask.registerOnResolveHandler(() => {
2080
+ _clearCurrentTask({
2081
+ taskID: task.id(),
2082
+ componentIdx: task.componentIdx(),
2083
+ });
2084
+ });
2085
+
2086
+ _setGlobalCurrentTaskMeta({
2087
+ componentIdx,
2088
+ taskID: newTaskID,
2089
+ });
2090
+
2091
+ meta = _getGlobalCurrentTaskMeta(componentIdx);
2092
+ }
2093
+
2094
+ const { taskID } = meta;
2055
2095
 
2056
2096
  const taskMeta = getCurrentTask(componentIdx, taskID);
2057
- if (!taskMeta) { throw new Error('invalid/missing async task meta'); }
2097
+ if (!taskMeta) {
2098
+ throw new Error('invalid/missing async task meta');
2099
+ }
2058
2100
 
2059
2101
  const task = taskMeta.task;
2060
2102
  if (!task) { throw new Error('invalid/missing async task'); }
@@ -2098,13 +2140,20 @@ function _lowerImportBackwardsCompat(args) {
2098
2140
  // TODO(breaking): remove once we get rid of manual async import specification,
2099
2141
  // as func types cannot be detected in that case only (and we don't need that w/ p3)
2100
2142
  if (!isManualAsync && !isAsync && !funcTypeIsAsync) {
2143
+ if (createdTask) { createdTask.enterSync(); }
2144
+
2101
2145
  const res = importFn(...params);
2146
+
2102
2147
  // TODO(breaking): remove once we get rid of manual async import specification,
2103
2148
  // as func types cannot be detected in that case only (and we don't need that w/ p3)
2104
2149
  if (!funcTypeIsAsync && !subtask.isReturned()) {
2105
2150
  throw new Error('post-execution subtasks must either be async or returned');
2106
2151
  }
2107
- return subtask.getResult();
2152
+
2153
+ const syncRes = subtask.getResult();
2154
+ if (createdTask) { createdTask.resolve([syncRes]); }
2155
+
2156
+ return syncRes;
2108
2157
  }
2109
2158
 
2110
2159
  // Sync-lowered async functions requires async behavior because the callee *can* block,
@@ -2173,10 +2222,16 @@ function _lowerImportBackwardsCompat(args) {
2173
2222
  queueMicrotask(async () => {
2174
2223
  try {
2175
2224
  _debugLog('[_lowerImportBackwardsCompat()] calling lowered import', { importFn, params });
2176
- const res = await importFn(...params);
2225
+ if (createdTask) { await createdTask.enter(); }
2226
+
2227
+ const asyncRes = await importFn(...params);
2177
2228
  if (requiresManualAsyncResult) {
2178
2229
  manualAsyncResult.resolve(subtask.getResult());
2179
2230
  }
2231
+
2232
+ if (createdTask) { createdTask.resolve([asyncRes]); }
2233
+
2234
+
2180
2235
  } catch (err) {
2181
2236
  _debugLog("[_lowerImportBackwardsCompat()] import fn error:", err);
2182
2237
  if (requiresManualAsyncResult) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bytecodealliance/jco",
3
- "version": "1.17.4",
3
+ "version": "1.17.5",
4
4
  "description": "JavaScript tooling for working with WebAssembly Components",
5
5
  "keywords": [
6
6
  "Component",