Skip to content

Commit

Permalink
Merge pull request #24 from extism/update-namespace2
Browse files Browse the repository at this point in the history
chore: update Extism namespace to `extism:host/env`
  • Loading branch information
nilslice authored Nov 12, 2023
2 parents b093fc0 + bcdcd4a commit a49d411
Show file tree
Hide file tree
Showing 22 changed files with 164 additions and 109 deletions.
2 changes: 1 addition & 1 deletion examples/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
Expand Down
8 changes: 4 additions & 4 deletions src/background-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,9 @@ class HttpContext {

contribute(functions: Record<string, Record<string, any>>) {
functions[EXTISM_ENV] ??= {};
functions[EXTISM_ENV].extism_http_request = (callContext: CallContext, reqaddr: bigint, bodyaddr: bigint) =>
functions[EXTISM_ENV].http_request = (callContext: CallContext, reqaddr: bigint, bodyaddr: bigint) =>
this.makeRequest(callContext, reqaddr, bodyaddr);
functions[EXTISM_ENV].extism_http_status_code = () => this.lastStatusCode;
functions[EXTISM_ENV].http_status_code = () => this.lastStatusCode;
}

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

const { header, url: rawUrl, method } = req.json();
let { header, url: rawUrl, method } = req.json();
method ??= 'GET';
const url = new URL(rawUrl);

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

const body = bodyaddr === 0n || method === 'GET' || method === 'HEAD' ? null : callContext.read(bodyaddr)?.bytes();
const fetch = this.fetch;

const response = await fetch(rawUrl, {
headers: header,
method,
Expand Down
42 changes: 21 additions & 21 deletions src/call-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,57 +154,57 @@ export class CallContext {

/** @hidden */
[ENV] = {
extism_alloc: (n: bigint): bigint => {
alloc: (n: bigint): bigint => {
return this.alloc(n);
},

extism_free: (addr: number) => {
free: (addr: number) => {
this.#blocks[Block.addressToIndex(addr)] = null;
},

extism_load_u8: (addr: bigint): number => {
load_u8: (addr: bigint): number => {
const blockIdx = Block.addressToIndex(addr);
const offset = Block.maskAddress(addr);
const block = this.#blocks[blockIdx];
return block?.view.getUint8(Number(offset)) as number;
},

extism_load_u64: (addr: bigint): bigint => {
load_u64: (addr: bigint): bigint => {
const blockIdx = Block.addressToIndex(addr);
const offset = Block.maskAddress(addr);
const block = this.#blocks[blockIdx];
return block?.view.getBigUint64(Number(offset), true) as bigint;
},

extism_store_u8: (addr: bigint, n: number) => {
store_u8: (addr: bigint, n: number) => {
const blockIdx = Block.addressToIndex(addr);
const offset = Block.maskAddress(addr);
const block = this.#blocks[blockIdx];
block?.view.setUint8(Number(offset), Number(n));
},

extism_store_u64: (addr: bigint, n: bigint) => {
store_u64: (addr: bigint, n: bigint) => {
const blockIdx = Block.addressToIndex(addr);
const offset = Block.maskAddress(addr);
const block = this.#blocks[blockIdx];
block?.view.setBigUint64(Number(offset), n, true);
},

extism_input_length: () => {
input_length: () => {
return BigInt(this.#input?.byteLength ?? 0);
},

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

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

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

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

extism_config_get: (addr: bigint): bigint => {
config_get: (addr: bigint): bigint => {
const item = this.read(addr);

if (item === null) {
Expand All @@ -244,7 +244,7 @@ export class CallContext {
return 0n;
},

extism_var_get: (addr: bigint): bigint => {
var_get: (addr: bigint): bigint => {
const item = this.read(addr);

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

extism_var_set: (addr: bigint, valueaddr: bigint) => {
var_set: (addr: bigint, valueaddr: bigint) => {
const item = this.read(addr);

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

extism_http_request: (_requestOffset: bigint, _bodyOffset: bigint): bigint => {
http_request: (_requestOffset: bigint, _bodyOffset: bigint): bigint => {
this.#logger.error('http_request is not enabled');
return 0n;
},

extism_http_status_code: (): number => {
http_status_code: (): number => {
this.#logger.error('http_status_code is not enabled');
return 0;
},

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

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

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

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

extism_log_error: (addr: bigint) => {
log_error: (addr: bigint) => {
const blockIdx = Block.addressToIndex(addr);
const block = this.#blocks[blockIdx];
if (!block) {
Expand Down
24 changes: 9 additions & 15 deletions src/foreground-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CallContext, RESET, GET_BLOCK, BEGIN, END, ENV, STORE } from './call-co
import { PluginOutput, type InternalConfig } from './interfaces.ts';
import { loadWasi } from 'js-sdk:wasi';

export const EXTISM_ENV = 'env';
export const EXTISM_ENV = 'extism:host/env';

export class ForegroundPlugin {
#context: CallContext;
Expand Down Expand Up @@ -161,6 +161,7 @@ export async function createForegroundPlugin(
const imports: Record<string, Record<string, any>> = {
...(wasi ? { wasi_snapshot_preview1: await wasi.importObject() } : {}),
[EXTISM_ENV]: context[ENV],
env: {},
};

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

const guestType = module.instance.exports._initialize
? 'reactor'
: module.instance.exports.hs_init
const guestType = module.instance.exports.hs_init
? 'haskell'
: module.instance.exports.__wasm_call_ctors
: module.instance.exports._initialize
? 'reactor'
: module.instance.exports._start
? 'command'
: 'none';

const init: any = module.instance.exports._initialize
? module.instance.exports._initialize
: module.instance.exports.hs_init
? module.instance.exports.hs_init
: module.instance.exports.__wasm_call_ctors
? module.instance.exports.__wasm_call_ctors
: () => {};

init();
const initRuntime: any = module.instance.exports.hs_init ? module.instance.exports.hs_init : () => {};
initRuntime();

return { module, guestType };
}),
Expand Down
Loading

0 comments on commit a49d411

Please sign in to comment.