Skip to content

chore: update Extism namespace to extism:host/env #24

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

Merged
merged 26 commits into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
1b01fa7
wip
chrisdickinson Oct 20, 2023
6148d94
feat: add bun support
chrisdickinson Oct 26, 2023
da240a2
fix: remove paths with ":"; windows does not support them
chrisdickinson Oct 27, 2023
9a4dbbe
rename "offMainThread" -> "runInWorker"
chrisdickinson Oct 27, 2023
9a6757b
fix: add formatting, lint examples, skip tests if WebAssembly is not …
chrisdickinson Oct 27, 2023
a2e0cdb
doc: improve typedocs; rename features->capabilities
chrisdickinson Oct 27, 2023
0b802a2
feat: implement PluginOutput + extism var get/set
chrisdickinson Oct 30, 2023
fd7d5dd
feat: implement basic http calls
chrisdickinson Oct 31, 2023
492ad65
fix: lint & format
chrisdickinson Oct 31, 2023
665374f
fix(test): add test for host fn calls that span many SAB pages
chrisdickinson Nov 2, 2023
5bd9fef
wip: update extism namespaces
zshipko Nov 7, 2023
9840de3
wip: update extism namespaces
zshipko Nov 8, 2023
508f1fc
wip: update extism namespaces
zshipko Nov 8, 2023
ba9fcdb
wip: update extism namespaces
zshipko Nov 8, 2023
e61263b
wip: update extism namespaces
zshipko Nov 8, 2023
0bc8eca
fix: http_request when method is null
zshipko Nov 8, 2023
e232cb5
fix: update browser wasi
zshipko Nov 8, 2023
5d65887
cleanup: avoid changing internel namespace
zshipko Nov 9, 2023
33d081f
cleanup: fix rebase
zshipko Nov 9, 2023
2620f24
fix: uncomment line
zshipko Nov 9, 2023
1d08b24
cleanup: improve deno wasi
zshipko Nov 9, 2023
91c0189
cleanup: use deno wasi init style code
zshipko Nov 9, 2023
c721753
cleanup: only call context.initialize when it exists
zshipko Nov 9, 2023
19daea0
Update src/call-context.ts
zshipko Nov 12, 2023
db7fee0
chore: run formatting
nilslice Nov 12, 2023
bcdcd4a
Merge pull request #26 from extism/update-namespace3
nilslice Nov 12, 2023
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
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