Skip to content

Commit a49d411

Browse files
authored
Merge pull request #24 from extism/update-namespace2
chore: update Extism namespace to `extism:host/env`
2 parents b093fc0 + bcdcd4a commit a49d411

22 files changed

+164
-109
lines changed

examples/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!DOCTYPE html>
1+
<!doctype html>
22
<html lang="en">
33
<head>
44
<meta charset="UTF-8" />

src/background-plugin.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,9 @@ class HttpContext {
308308

309309
contribute(functions: Record<string, Record<string, any>>) {
310310
functions[EXTISM_ENV] ??= {};
311-
functions[EXTISM_ENV].extism_http_request = (callContext: CallContext, reqaddr: bigint, bodyaddr: bigint) =>
311+
functions[EXTISM_ENV].http_request = (callContext: CallContext, reqaddr: bigint, bodyaddr: bigint) =>
312312
this.makeRequest(callContext, reqaddr, bodyaddr);
313-
functions[EXTISM_ENV].extism_http_status_code = () => this.lastStatusCode;
313+
functions[EXTISM_ENV].http_status_code = () => this.lastStatusCode;
314314
}
315315

316316
async makeRequest(callContext: CallContext, reqaddr: bigint, bodyaddr: bigint) {
@@ -319,7 +319,8 @@ class HttpContext {
319319
return 0n;
320320
}
321321

322-
const { header, url: rawUrl, method } = req.json();
322+
let { header, url: rawUrl, method } = req.json();
323+
method ??= 'GET';
323324
const url = new URL(rawUrl);
324325

325326
const isAllowed = this.allowedHosts.some((allowedHost) => {
@@ -332,7 +333,6 @@ class HttpContext {
332333

333334
const body = bodyaddr === 0n || method === 'GET' || method === 'HEAD' ? null : callContext.read(bodyaddr)?.bytes();
334335
const fetch = this.fetch;
335-
336336
const response = await fetch(rawUrl, {
337337
headers: header,
338338
method,

src/call-context.ts

+21-21
Original file line numberDiff line numberDiff line change
@@ -154,57 +154,57 @@ export class CallContext {
154154

155155
/** @hidden */
156156
[ENV] = {
157-
extism_alloc: (n: bigint): bigint => {
157+
alloc: (n: bigint): bigint => {
158158
return this.alloc(n);
159159
},
160160

161-
extism_free: (addr: number) => {
161+
free: (addr: number) => {
162162
this.#blocks[Block.addressToIndex(addr)] = null;
163163
},
164164

165-
extism_load_u8: (addr: bigint): number => {
165+
load_u8: (addr: bigint): number => {
166166
const blockIdx = Block.addressToIndex(addr);
167167
const offset = Block.maskAddress(addr);
168168
const block = this.#blocks[blockIdx];
169169
return block?.view.getUint8(Number(offset)) as number;
170170
},
171171

172-
extism_load_u64: (addr: bigint): bigint => {
172+
load_u64: (addr: bigint): bigint => {
173173
const blockIdx = Block.addressToIndex(addr);
174174
const offset = Block.maskAddress(addr);
175175
const block = this.#blocks[blockIdx];
176176
return block?.view.getBigUint64(Number(offset), true) as bigint;
177177
},
178178

179-
extism_store_u8: (addr: bigint, n: number) => {
179+
store_u8: (addr: bigint, n: number) => {
180180
const blockIdx = Block.addressToIndex(addr);
181181
const offset = Block.maskAddress(addr);
182182
const block = this.#blocks[blockIdx];
183183
block?.view.setUint8(Number(offset), Number(n));
184184
},
185185

186-
extism_store_u64: (addr: bigint, n: bigint) => {
186+
store_u64: (addr: bigint, n: bigint) => {
187187
const blockIdx = Block.addressToIndex(addr);
188188
const offset = Block.maskAddress(addr);
189189
const block = this.#blocks[blockIdx];
190190
block?.view.setBigUint64(Number(offset), n, true);
191191
},
192192

193-
extism_input_length: () => {
193+
input_length: () => {
194194
return BigInt(this.#input?.byteLength ?? 0);
195195
},
196196

197-
extism_input_load_u8: (addr: bigint): number => {
197+
input_load_u8: (addr: bigint): number => {
198198
const offset = Block.maskAddress(addr);
199199
return this.#input?.view.getUint8(Number(offset)) as number;
200200
},
201201

202-
extism_input_load_u64: (addr: bigint): bigint => {
202+
input_load_u64: (addr: bigint): bigint => {
203203
const offset = Block.maskAddress(addr);
204204
return this.#input?.view.getBigUint64(Number(offset), true) as bigint;
205205
},
206206

207-
extism_output_set: (addr: bigint, length: bigint) => {
207+
output_set: (addr: bigint, length: bigint) => {
208208
const blockIdx = Block.addressToIndex(addr);
209209
const block = this.#blocks[blockIdx];
210210
if (!block) {
@@ -218,7 +218,7 @@ export class CallContext {
218218
this.#stack[this.#stack.length - 1][1] = blockIdx;
219219
},
220220

221-
extism_error_set: (addr: bigint) => {
221+
error_set: (addr: bigint) => {
222222
const blockIdx = Block.addressToIndex(addr);
223223
const block = this.#blocks[blockIdx];
224224
if (!block) {
@@ -228,7 +228,7 @@ export class CallContext {
228228
this.#stack[this.#stack.length - 1][2] = blockIdx;
229229
},
230230

231-
extism_config_get: (addr: bigint): bigint => {
231+
config_get: (addr: bigint): bigint => {
232232
const item = this.read(addr);
233233

234234
if (item === null) {
@@ -244,7 +244,7 @@ export class CallContext {
244244
return 0n;
245245
},
246246

247-
extism_var_get: (addr: bigint): bigint => {
247+
var_get: (addr: bigint): bigint => {
248248
const item = this.read(addr);
249249

250250
if (item === null) {
@@ -255,7 +255,7 @@ export class CallContext {
255255
return this.#vars.has(key) ? Block.indexToAddress(this.#vars.get(key) as number) : 0n;
256256
},
257257

258-
extism_var_set: (addr: bigint, valueaddr: bigint) => {
258+
var_set: (addr: bigint, valueaddr: bigint) => {
259259
const item = this.read(addr);
260260

261261
if (item === null) {
@@ -271,17 +271,17 @@ export class CallContext {
271271
this.#vars.set(key, Block.addressToIndex(valueaddr));
272272
},
273273

274-
extism_http_request: (_requestOffset: bigint, _bodyOffset: bigint): bigint => {
274+
http_request: (_requestOffset: bigint, _bodyOffset: bigint): bigint => {
275275
this.#logger.error('http_request is not enabled');
276276
return 0n;
277277
},
278278

279-
extism_http_status_code: (): number => {
279+
http_status_code: (): number => {
280280
this.#logger.error('http_status_code is not enabled');
281281
return 0;
282282
},
283283

284-
extism_length: (addr: bigint): bigint => {
284+
length: (addr: bigint): bigint => {
285285
const blockIdx = Block.addressToIndex(addr);
286286
const block = this.#blocks[blockIdx];
287287
if (!block) {
@@ -290,7 +290,7 @@ export class CallContext {
290290
return BigInt(block.buffer.byteLength);
291291
},
292292

293-
extism_log_warn: (addr: bigint) => {
293+
log_warn: (addr: bigint) => {
294294
const blockIdx = Block.addressToIndex(addr);
295295
const block = this.#blocks[blockIdx];
296296
if (!block) {
@@ -302,7 +302,7 @@ export class CallContext {
302302
this.#logger.warn(text);
303303
},
304304

305-
extism_log_info: (addr: bigint) => {
305+
log_info: (addr: bigint) => {
306306
const blockIdx = Block.addressToIndex(addr);
307307
const block = this.#blocks[blockIdx];
308308
if (!block) {
@@ -314,7 +314,7 @@ export class CallContext {
314314
this.#logger.info(text);
315315
},
316316

317-
extism_log_debug: (addr: bigint) => {
317+
log_debug: (addr: bigint) => {
318318
const blockIdx = Block.addressToIndex(addr);
319319
const block = this.#blocks[blockIdx];
320320
if (!block) {
@@ -326,7 +326,7 @@ export class CallContext {
326326
this.#logger.debug(text);
327327
},
328328

329-
extism_log_error: (addr: bigint) => {
329+
log_error: (addr: bigint) => {
330330
const blockIdx = Block.addressToIndex(addr);
331331
const block = this.#blocks[blockIdx];
332332
if (!block) {

src/foreground-plugin.ts

+9-15
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { CallContext, RESET, GET_BLOCK, BEGIN, END, ENV, STORE } from './call-co
22
import { PluginOutput, type InternalConfig } from './interfaces.ts';
33
import { loadWasi } from 'js-sdk:wasi';
44

5-
export const EXTISM_ENV = 'env';
5+
export const EXTISM_ENV = 'extism:host/env';
66

77
export class ForegroundPlugin {
88
#context: CallContext;
@@ -161,6 +161,7 @@ export async function createForegroundPlugin(
161161
const imports: Record<string, Record<string, any>> = {
162162
...(wasi ? { wasi_snapshot_preview1: await wasi.importObject() } : {}),
163163
[EXTISM_ENV]: context[ENV],
164+
env: {},
164165
};
165166

166167
for (const namespace in opts.functions) {
@@ -173,27 +174,20 @@ export async function createForegroundPlugin(
173174
const modules = await Promise.all(
174175
sources.map(async (source) => {
175176
const module = await WebAssembly.instantiate(source, imports);
176-
if (wasi && module.instance.exports._start) {
177+
if (wasi) {
177178
await wasi?.initialize(module.instance);
178179
}
179180

180-
const guestType = module.instance.exports._initialize
181-
? 'reactor'
182-
: module.instance.exports.hs_init
181+
const guestType = module.instance.exports.hs_init
183182
? 'haskell'
184-
: module.instance.exports.__wasm_call_ctors
183+
: module.instance.exports._initialize
184+
? 'reactor'
185+
: module.instance.exports._start
185186
? 'command'
186187
: 'none';
187188

188-
const init: any = module.instance.exports._initialize
189-
? module.instance.exports._initialize
190-
: module.instance.exports.hs_init
191-
? module.instance.exports.hs_init
192-
: module.instance.exports.__wasm_call_ctors
193-
? module.instance.exports.__wasm_call_ctors
194-
: () => {};
195-
196-
init();
189+
const initRuntime: any = module.instance.exports.hs_init ? module.instance.exports.hs_init : () => {};
190+
initRuntime();
197191

198192
return { module, guestType };
199193
}),

0 commit comments

Comments
 (0)