@blueshift-gg/sbpf-assembler 0.1.6

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.
package/README.md ADDED
@@ -0,0 +1,198 @@
1
+ ## Table of Contents
2
+
3
+ - [sbpf](#sbpf)
4
+ - [Installation](#installation)
5
+ - [Usage](#usage)
6
+ - [Command Details](#command-details)
7
+ - [Initialize a Project](#initialize-a-project)
8
+ - [Examples](#examples)
9
+ - [Create a new project with Rust tests (default)](#create-a-new-project-with-rust-tests-default)
10
+ - [Create a new project with TypeScript tests](#create-a-new-project-with-typescript-tests)
11
+ - [Disassembler](#disassembler)
12
+ - [Debugger](#debugger)
13
+ - [Advanced Usage](#advanced-usage)
14
+ - [License](#license)
15
+ - [Contributing](#contributing)
16
+
17
+ # sbpf
18
+
19
+ ![ci](https://github.com/blueshift-gg/sbpf/actions/workflows/ci.yml/badge.svg)
20
+ [![codecov](https://codecov.io/gh/blueshift-gg/sbpf/branch/master/graph/badge.svg)](https://codecov.io/gh/blueshift-gg/sbpf)
21
+
22
+ A simple scaffold to bootstrap sBPF Assembly programs.
23
+
24
+ ### Installation
25
+
26
+ ```sh
27
+ cargo install --git https://github.com/blueshift-gg/sbpf.git
28
+ ```
29
+
30
+ ### Usage
31
+
32
+ To view all the commands you can run, type `sbpf help`. Here are the available commands:
33
+
34
+ - `init`: Create a new project scaffold.
35
+ - `build`: Compile into a Solana program executable.
36
+ - `deploy`: Build and deploy the program.
37
+ - `test`: Test the deployed program.
38
+ - `e2e`: Build, deploy, and test a program.
39
+ - `clean`: Clean up build and deploy artifacts.
40
+ - `disassemble`: Disassemble a Solana program executable.
41
+ - `debug`: Debug an sBPF assembly program.
42
+ - `help`: Print this message or the help of the given subcommand(s).
43
+
44
+ ```
45
+ Usage: sbpf <COMMAND>
46
+
47
+ Commands:
48
+ init Create a new project scaffold
49
+ build Compile into a Solana program executable
50
+ deploy Build and deploy the program
51
+ test Test deployed program
52
+ e2e Build, deploy and test a program
53
+ clean Clean up build and deploy artifacts
54
+ disassemble Disassemble a Solana program executable
55
+ debug Debug an sBPF assembly program
56
+ help Print this message or the help of the given subcommand(s)
57
+
58
+ Options:
59
+ -h, --help Print help
60
+ -V, --version Print version
61
+ ```
62
+
63
+ ### Command Details
64
+
65
+ #### Initialize a Project
66
+
67
+ To create a new project, use the `sbpf init` command. By default, it initializes a project with Rust tests using [Mollusk](https://github.com/buffalojoec/mollusk). You can also initialize a project with TypeScript tests using the `--ts-tests` option.
68
+
69
+ ```sh
70
+ sbpf init --help
71
+ Create a new project scaffold
72
+
73
+ Usage: sbpf init [OPTIONS] [NAME]
74
+
75
+ Arguments:
76
+ [NAME] The name of the project to create
77
+
78
+ Options:
79
+ -t, --ts-tests Initialize with TypeScript tests instead of Mollusk Rust tests
80
+ -h, --help Print help information
81
+ -V, --version Print version information
82
+ ```
83
+
84
+ ##### Examples
85
+
86
+ ###### Create a new project with Rust tests (default)
87
+
88
+ ```sh
89
+ sbpf init my-project
90
+ ```
91
+
92
+ ###### Create a new project with TypeScript tests
93
+
94
+ ```sh
95
+ sbpf init my-project --ts-tests
96
+ ```
97
+
98
+ After initializing the project, you can navigate into the project directory and use other commands to build, deploy, and test your program.
99
+
100
+ #### Disassembler
101
+
102
+ The disassembler converts a Solana program executable (ELF) into human-readable sBPF assembly.
103
+
104
+ ```sh
105
+ sbpf disassemble <FILENAME>
106
+ ```
107
+
108
+ #### Debugger
109
+
110
+ The debugger provides an interactive REPL for stepping through sBPF assembly programs.
111
+
112
+ *Debug an assembly file:*
113
+
114
+ ```sh
115
+ sbpf debug --asm <FILENAME>
116
+ ```
117
+
118
+ *Debug an ELF file:*
119
+
120
+ ```sh
121
+ sbpf debug --elf <FILENAME>
122
+ ```
123
+
124
+ *Input:*
125
+
126
+ To debug programs that require input, the debugger accepts a JSON file (or JSON string) containing the instruction being executed and the accounts involved. Pass it using the `--input` flag:
127
+
128
+ ```sh
129
+ sbpf debug --asm src/my-program/my-program.s --input input.json
130
+ ```
131
+
132
+ The JSON should contain the following information:
133
+
134
+ - **`instruction`**: The instruction to execute, including the program ID, account metas, and instruction data.
135
+ - **`accounts`**: The account states. The `data` field in each account and instruction should be base58 encoded.
136
+
137
+
138
+ *Example:*
139
+ ```json
140
+ {
141
+ "instruction": {
142
+ "program_id": "78ycAjmvvq2Xjz6mBgGTsuHHNVADZ75NWgXKPY8wvF2s",
143
+ "accounts": [
144
+ {
145
+ "pubkey": "3JF3sEqM796hk5WFqA6EtmEwJQ9quALszsfJyvXNQKy3",
146
+ "is_signer": true,
147
+ "is_writable": false
148
+ },
149
+ {
150
+ "pubkey": "11157t3sqMV725NVRLrVQbAu98Jjfk1uCKehJnXXQs",
151
+ "is_signer": false,
152
+ "is_writable": true
153
+ }
154
+ ],
155
+ "data": "8AQGAut7N95oMfV99bhRZ"
156
+ },
157
+ "accounts": [
158
+ {
159
+ "pubkey": "3JF3sEqM796hk5WFqA6EtmEwJQ9quALszsfJyvXNQKy3",
160
+ "owner": "11111111111111111111111111111111",
161
+ "lamports": 1000000000,
162
+ "data": "",
163
+ "executable": false
164
+ },
165
+ {
166
+ "pubkey": "11157t3sqMV725NVRLrVQbAu98Jjfk1uCKehJnXXQs",
167
+ "owner": "11111111111111111111111111111111",
168
+ "lamports": 1000000000,
169
+ "data": "",
170
+ "executable": false
171
+ }
172
+ ]
173
+ }
174
+ ```
175
+
176
+
177
+
178
+ ### Advanced Usage
179
+
180
+ You can override the default linker with a [custom linker file](https://github.com/deanmlittle/sbpf-asm-noop/blob/master/src/noop/noop.ld) by including it in the src directory with the same name as your program. For example:
181
+
182
+ ```
183
+ src/example/example.s
184
+ src/example/example.ld
185
+ ```
186
+
187
+ ### License
188
+
189
+ Licensed under either of
190
+
191
+ * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
192
+ * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
193
+
194
+ at your option.
195
+
196
+ ### Contributing
197
+
198
+ PRs welcome!
@@ -0,0 +1,4 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ export function assemble(source: string, arch: number): Uint8Array;
@@ -0,0 +1,9 @@
1
+ /* @ts-self-types="./sbpf_assembler.d.ts" */
2
+
3
+ import * as wasm from "./sbpf_assembler_bg.wasm";
4
+ import { __wbg_set_wasm } from "./sbpf_assembler_bg.js";
5
+ __wbg_set_wasm(wasm);
6
+ wasm.__wbindgen_start();
7
+ export {
8
+ assemble
9
+ } from "./sbpf_assembler_bg.js";
@@ -0,0 +1,222 @@
1
+ /**
2
+ * @param {string} source
3
+ * @param {number} arch
4
+ * @returns {Uint8Array}
5
+ */
6
+ export function assemble(source, arch) {
7
+ const ptr0 = passStringToWasm0(source, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
8
+ const len0 = WASM_VECTOR_LEN;
9
+ const ret = wasm.assemble(ptr0, len0, arch);
10
+ if (ret[3]) {
11
+ throw takeFromExternrefTable0(ret[2]);
12
+ }
13
+ var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice();
14
+ wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
15
+ return v2;
16
+ }
17
+ export function __wbg___wbindgen_debug_string_a1b3fd0656850da8(arg0, arg1) {
18
+ const ret = debugString(arg1);
19
+ const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
20
+ const len1 = WASM_VECTOR_LEN;
21
+ getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
22
+ getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
23
+ }
24
+ export function __wbg___wbindgen_throw_83ebd457a191bc2a(arg0, arg1) {
25
+ throw new Error(getStringFromWasm0(arg0, arg1));
26
+ }
27
+ export function __wbg_new_2e5db3ea23dcc1a1() {
28
+ const ret = new Array();
29
+ return ret;
30
+ }
31
+ export function __wbg_new_5c365a7570baea64() {
32
+ const ret = new Object();
33
+ return ret;
34
+ }
35
+ export function __wbg_set_6be42768c690e380(arg0, arg1, arg2) {
36
+ arg0[arg1] = arg2;
37
+ }
38
+ export function __wbg_set_c7d56bae406212ae(arg0, arg1, arg2) {
39
+ arg0[arg1 >>> 0] = arg2;
40
+ }
41
+ export function __wbindgen_cast_0000000000000001(arg0, arg1) {
42
+ // Cast intrinsic for `Ref(String) -> Externref`.
43
+ const ret = getStringFromWasm0(arg0, arg1);
44
+ return ret;
45
+ }
46
+ export function __wbindgen_init_externref_table() {
47
+ const table = wasm.__wbindgen_externrefs;
48
+ const offset = table.grow(4);
49
+ table.set(0, undefined);
50
+ table.set(offset + 0, undefined);
51
+ table.set(offset + 1, null);
52
+ table.set(offset + 2, true);
53
+ table.set(offset + 3, false);
54
+ }
55
+ function debugString(val) {
56
+ // primitive types
57
+ const type = typeof val;
58
+ if (type == 'number' || type == 'boolean' || val == null) {
59
+ return `${val}`;
60
+ }
61
+ if (type == 'string') {
62
+ return `"${val}"`;
63
+ }
64
+ if (type == 'symbol') {
65
+ const description = val.description;
66
+ if (description == null) {
67
+ return 'Symbol';
68
+ } else {
69
+ return `Symbol(${description})`;
70
+ }
71
+ }
72
+ if (type == 'function') {
73
+ const name = val.name;
74
+ if (typeof name == 'string' && name.length > 0) {
75
+ return `Function(${name})`;
76
+ } else {
77
+ return 'Function';
78
+ }
79
+ }
80
+ // objects
81
+ if (Array.isArray(val)) {
82
+ const length = val.length;
83
+ let debug = '[';
84
+ if (length > 0) {
85
+ debug += debugString(val[0]);
86
+ }
87
+ for(let i = 1; i < length; i++) {
88
+ debug += ', ' + debugString(val[i]);
89
+ }
90
+ debug += ']';
91
+ return debug;
92
+ }
93
+ // Test for built-in
94
+ const builtInMatches = /\[object ([^\]]+)\]/.exec(toString.call(val));
95
+ let className;
96
+ if (builtInMatches && builtInMatches.length > 1) {
97
+ className = builtInMatches[1];
98
+ } else {
99
+ // Failed to match the standard '[object ClassName]'
100
+ return toString.call(val);
101
+ }
102
+ if (className == 'Object') {
103
+ // we're a user defined class or Object
104
+ // JSON.stringify avoids problems with cycles, and is generally much
105
+ // easier than looping through ownProperties of `val`.
106
+ try {
107
+ return 'Object(' + JSON.stringify(val) + ')';
108
+ } catch (_) {
109
+ return 'Object';
110
+ }
111
+ }
112
+ // errors
113
+ if (val instanceof Error) {
114
+ return `${val.name}: ${val.message}\n${val.stack}`;
115
+ }
116
+ // TODO we could test for more things here, like `Set`s and `Map`s.
117
+ return className;
118
+ }
119
+
120
+ function getArrayU8FromWasm0(ptr, len) {
121
+ ptr = ptr >>> 0;
122
+ return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
123
+ }
124
+
125
+ let cachedDataViewMemory0 = null;
126
+ function getDataViewMemory0() {
127
+ if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
128
+ cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
129
+ }
130
+ return cachedDataViewMemory0;
131
+ }
132
+
133
+ function getStringFromWasm0(ptr, len) {
134
+ ptr = ptr >>> 0;
135
+ return decodeText(ptr, len);
136
+ }
137
+
138
+ let cachedUint8ArrayMemory0 = null;
139
+ function getUint8ArrayMemory0() {
140
+ if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
141
+ cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
142
+ }
143
+ return cachedUint8ArrayMemory0;
144
+ }
145
+
146
+ function passStringToWasm0(arg, malloc, realloc) {
147
+ if (realloc === undefined) {
148
+ const buf = cachedTextEncoder.encode(arg);
149
+ const ptr = malloc(buf.length, 1) >>> 0;
150
+ getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
151
+ WASM_VECTOR_LEN = buf.length;
152
+ return ptr;
153
+ }
154
+
155
+ let len = arg.length;
156
+ let ptr = malloc(len, 1) >>> 0;
157
+
158
+ const mem = getUint8ArrayMemory0();
159
+
160
+ let offset = 0;
161
+
162
+ for (; offset < len; offset++) {
163
+ const code = arg.charCodeAt(offset);
164
+ if (code > 0x7F) break;
165
+ mem[ptr + offset] = code;
166
+ }
167
+ if (offset !== len) {
168
+ if (offset !== 0) {
169
+ arg = arg.slice(offset);
170
+ }
171
+ ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
172
+ const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
173
+ const ret = cachedTextEncoder.encodeInto(arg, view);
174
+
175
+ offset += ret.written;
176
+ ptr = realloc(ptr, len, offset, 1) >>> 0;
177
+ }
178
+
179
+ WASM_VECTOR_LEN = offset;
180
+ return ptr;
181
+ }
182
+
183
+ function takeFromExternrefTable0(idx) {
184
+ const value = wasm.__wbindgen_externrefs.get(idx);
185
+ wasm.__externref_table_dealloc(idx);
186
+ return value;
187
+ }
188
+
189
+ let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
190
+ cachedTextDecoder.decode();
191
+ const MAX_SAFARI_DECODE_BYTES = 2146435072;
192
+ let numBytesDecoded = 0;
193
+ function decodeText(ptr, len) {
194
+ numBytesDecoded += len;
195
+ if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
196
+ cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
197
+ cachedTextDecoder.decode();
198
+ numBytesDecoded = len;
199
+ }
200
+ return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
201
+ }
202
+
203
+ const cachedTextEncoder = new TextEncoder();
204
+
205
+ if (!('encodeInto' in cachedTextEncoder)) {
206
+ cachedTextEncoder.encodeInto = function (arg, view) {
207
+ const buf = cachedTextEncoder.encode(arg);
208
+ view.set(buf);
209
+ return {
210
+ read: arg.length,
211
+ written: buf.length
212
+ };
213
+ };
214
+ }
215
+
216
+ let WASM_VECTOR_LEN = 0;
217
+
218
+
219
+ let wasm;
220
+ export function __wbg_set_wasm(val) {
221
+ wasm = val;
222
+ }
@@ -0,0 +1,10 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+ export const memory: WebAssembly.Memory;
4
+ export const assemble: (a: number, b: number, c: number) => [number, number, number, number];
5
+ export const __wbindgen_malloc: (a: number, b: number) => number;
6
+ export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
7
+ export const __wbindgen_externrefs: WebAssembly.Table;
8
+ export const __externref_table_dealloc: (a: number) => void;
9
+ export const __wbindgen_free: (a: number, b: number, c: number) => void;
10
+ export const __wbindgen_start: () => void;
@@ -0,0 +1,4 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ export function assemble(source: string, arch: number): Uint8Array;
@@ -0,0 +1,228 @@
1
+ /* @ts-self-types="./sbpf_assembler.d.ts" */
2
+
3
+ /**
4
+ * @param {string} source
5
+ * @param {number} arch
6
+ * @returns {Uint8Array}
7
+ */
8
+ function assemble(source, arch) {
9
+ const ptr0 = passStringToWasm0(source, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
10
+ const len0 = WASM_VECTOR_LEN;
11
+ const ret = wasm.assemble(ptr0, len0, arch);
12
+ if (ret[3]) {
13
+ throw takeFromExternrefTable0(ret[2]);
14
+ }
15
+ var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice();
16
+ wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
17
+ return v2;
18
+ }
19
+ exports.assemble = assemble;
20
+
21
+ function __wbg_get_imports() {
22
+ const import0 = {
23
+ __proto__: null,
24
+ __wbg___wbindgen_debug_string_a1b3fd0656850da8: function(arg0, arg1) {
25
+ const ret = debugString(arg1);
26
+ const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
27
+ const len1 = WASM_VECTOR_LEN;
28
+ getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
29
+ getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
30
+ },
31
+ __wbg___wbindgen_throw_83ebd457a191bc2a: function(arg0, arg1) {
32
+ throw new Error(getStringFromWasm0(arg0, arg1));
33
+ },
34
+ __wbg_new_2e5db3ea23dcc1a1: function() {
35
+ const ret = new Array();
36
+ return ret;
37
+ },
38
+ __wbg_new_5c365a7570baea64: function() {
39
+ const ret = new Object();
40
+ return ret;
41
+ },
42
+ __wbg_set_6be42768c690e380: function(arg0, arg1, arg2) {
43
+ arg0[arg1] = arg2;
44
+ },
45
+ __wbg_set_c7d56bae406212ae: function(arg0, arg1, arg2) {
46
+ arg0[arg1 >>> 0] = arg2;
47
+ },
48
+ __wbindgen_cast_0000000000000001: function(arg0, arg1) {
49
+ // Cast intrinsic for `Ref(String) -> Externref`.
50
+ const ret = getStringFromWasm0(arg0, arg1);
51
+ return ret;
52
+ },
53
+ __wbindgen_init_externref_table: function() {
54
+ const table = wasm.__wbindgen_externrefs;
55
+ const offset = table.grow(4);
56
+ table.set(0, undefined);
57
+ table.set(offset + 0, undefined);
58
+ table.set(offset + 1, null);
59
+ table.set(offset + 2, true);
60
+ table.set(offset + 3, false);
61
+ },
62
+ };
63
+ return {
64
+ __proto__: null,
65
+ "./sbpf_assembler_bg.js": import0,
66
+ };
67
+ }
68
+
69
+ function debugString(val) {
70
+ // primitive types
71
+ const type = typeof val;
72
+ if (type == 'number' || type == 'boolean' || val == null) {
73
+ return `${val}`;
74
+ }
75
+ if (type == 'string') {
76
+ return `"${val}"`;
77
+ }
78
+ if (type == 'symbol') {
79
+ const description = val.description;
80
+ if (description == null) {
81
+ return 'Symbol';
82
+ } else {
83
+ return `Symbol(${description})`;
84
+ }
85
+ }
86
+ if (type == 'function') {
87
+ const name = val.name;
88
+ if (typeof name == 'string' && name.length > 0) {
89
+ return `Function(${name})`;
90
+ } else {
91
+ return 'Function';
92
+ }
93
+ }
94
+ // objects
95
+ if (Array.isArray(val)) {
96
+ const length = val.length;
97
+ let debug = '[';
98
+ if (length > 0) {
99
+ debug += debugString(val[0]);
100
+ }
101
+ for(let i = 1; i < length; i++) {
102
+ debug += ', ' + debugString(val[i]);
103
+ }
104
+ debug += ']';
105
+ return debug;
106
+ }
107
+ // Test for built-in
108
+ const builtInMatches = /\[object ([^\]]+)\]/.exec(toString.call(val));
109
+ let className;
110
+ if (builtInMatches && builtInMatches.length > 1) {
111
+ className = builtInMatches[1];
112
+ } else {
113
+ // Failed to match the standard '[object ClassName]'
114
+ return toString.call(val);
115
+ }
116
+ if (className == 'Object') {
117
+ // we're a user defined class or Object
118
+ // JSON.stringify avoids problems with cycles, and is generally much
119
+ // easier than looping through ownProperties of `val`.
120
+ try {
121
+ return 'Object(' + JSON.stringify(val) + ')';
122
+ } catch (_) {
123
+ return 'Object';
124
+ }
125
+ }
126
+ // errors
127
+ if (val instanceof Error) {
128
+ return `${val.name}: ${val.message}\n${val.stack}`;
129
+ }
130
+ // TODO we could test for more things here, like `Set`s and `Map`s.
131
+ return className;
132
+ }
133
+
134
+ function getArrayU8FromWasm0(ptr, len) {
135
+ ptr = ptr >>> 0;
136
+ return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
137
+ }
138
+
139
+ let cachedDataViewMemory0 = null;
140
+ function getDataViewMemory0() {
141
+ if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
142
+ cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
143
+ }
144
+ return cachedDataViewMemory0;
145
+ }
146
+
147
+ function getStringFromWasm0(ptr, len) {
148
+ ptr = ptr >>> 0;
149
+ return decodeText(ptr, len);
150
+ }
151
+
152
+ let cachedUint8ArrayMemory0 = null;
153
+ function getUint8ArrayMemory0() {
154
+ if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
155
+ cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
156
+ }
157
+ return cachedUint8ArrayMemory0;
158
+ }
159
+
160
+ function passStringToWasm0(arg, malloc, realloc) {
161
+ if (realloc === undefined) {
162
+ const buf = cachedTextEncoder.encode(arg);
163
+ const ptr = malloc(buf.length, 1) >>> 0;
164
+ getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
165
+ WASM_VECTOR_LEN = buf.length;
166
+ return ptr;
167
+ }
168
+
169
+ let len = arg.length;
170
+ let ptr = malloc(len, 1) >>> 0;
171
+
172
+ const mem = getUint8ArrayMemory0();
173
+
174
+ let offset = 0;
175
+
176
+ for (; offset < len; offset++) {
177
+ const code = arg.charCodeAt(offset);
178
+ if (code > 0x7F) break;
179
+ mem[ptr + offset] = code;
180
+ }
181
+ if (offset !== len) {
182
+ if (offset !== 0) {
183
+ arg = arg.slice(offset);
184
+ }
185
+ ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
186
+ const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
187
+ const ret = cachedTextEncoder.encodeInto(arg, view);
188
+
189
+ offset += ret.written;
190
+ ptr = realloc(ptr, len, offset, 1) >>> 0;
191
+ }
192
+
193
+ WASM_VECTOR_LEN = offset;
194
+ return ptr;
195
+ }
196
+
197
+ function takeFromExternrefTable0(idx) {
198
+ const value = wasm.__wbindgen_externrefs.get(idx);
199
+ wasm.__externref_table_dealloc(idx);
200
+ return value;
201
+ }
202
+
203
+ let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
204
+ cachedTextDecoder.decode();
205
+ function decodeText(ptr, len) {
206
+ return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
207
+ }
208
+
209
+ const cachedTextEncoder = new TextEncoder();
210
+
211
+ if (!('encodeInto' in cachedTextEncoder)) {
212
+ cachedTextEncoder.encodeInto = function (arg, view) {
213
+ const buf = cachedTextEncoder.encode(arg);
214
+ view.set(buf);
215
+ return {
216
+ read: arg.length,
217
+ written: buf.length
218
+ };
219
+ };
220
+ }
221
+
222
+ let WASM_VECTOR_LEN = 0;
223
+
224
+ const wasmPath = `${__dirname}/sbpf_assembler_bg.wasm`;
225
+ const wasmBytes = require('fs').readFileSync(wasmPath);
226
+ const wasmModule = new WebAssembly.Module(wasmBytes);
227
+ let wasm = new WebAssembly.Instance(wasmModule, __wbg_get_imports()).exports;
228
+ wasm.__wbindgen_start();
Binary file
@@ -0,0 +1,10 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+ export const memory: WebAssembly.Memory;
4
+ export const assemble: (a: number, b: number, c: number) => [number, number, number, number];
5
+ export const __wbindgen_malloc: (a: number, b: number) => number;
6
+ export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
7
+ export const __wbindgen_externrefs: WebAssembly.Table;
8
+ export const __externref_table_dealloc: (a: number) => void;
9
+ export const __wbindgen_free: (a: number, b: number, c: number) => void;
10
+ export const __wbindgen_start: () => void;
@@ -0,0 +1,39 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ export function assemble(source: string, arch: number): Uint8Array;
5
+
6
+ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
7
+
8
+ export interface InitOutput {
9
+ readonly memory: WebAssembly.Memory;
10
+ readonly assemble: (a: number, b: number, c: number) => [number, number, number, number];
11
+ readonly __wbindgen_malloc: (a: number, b: number) => number;
12
+ readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
13
+ readonly __wbindgen_externrefs: WebAssembly.Table;
14
+ readonly __externref_table_dealloc: (a: number) => void;
15
+ readonly __wbindgen_free: (a: number, b: number, c: number) => void;
16
+ readonly __wbindgen_start: () => void;
17
+ }
18
+
19
+ export type SyncInitInput = BufferSource | WebAssembly.Module;
20
+
21
+ /**
22
+ * Instantiates the given `module`, which can either be bytes or
23
+ * a precompiled `WebAssembly.Module`.
24
+ *
25
+ * @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
26
+ *
27
+ * @returns {InitOutput}
28
+ */
29
+ export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
30
+
31
+ /**
32
+ * If `module_or_path` is {RequestInfo} or {URL}, makes a request and
33
+ * for everything else, calls `WebAssembly.instantiate` directly.
34
+ *
35
+ * @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
36
+ *
37
+ * @returns {Promise<InitOutput>}
38
+ */
39
+ export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;
@@ -0,0 +1,322 @@
1
+ /* @ts-self-types="./sbpf_assembler.d.ts" */
2
+
3
+ /**
4
+ * @param {string} source
5
+ * @param {number} arch
6
+ * @returns {Uint8Array}
7
+ */
8
+ export function assemble(source, arch) {
9
+ const ptr0 = passStringToWasm0(source, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
10
+ const len0 = WASM_VECTOR_LEN;
11
+ const ret = wasm.assemble(ptr0, len0, arch);
12
+ if (ret[3]) {
13
+ throw takeFromExternrefTable0(ret[2]);
14
+ }
15
+ var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice();
16
+ wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
17
+ return v2;
18
+ }
19
+
20
+ function __wbg_get_imports() {
21
+ const import0 = {
22
+ __proto__: null,
23
+ __wbg___wbindgen_debug_string_a1b3fd0656850da8: function(arg0, arg1) {
24
+ const ret = debugString(arg1);
25
+ const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
26
+ const len1 = WASM_VECTOR_LEN;
27
+ getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
28
+ getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
29
+ },
30
+ __wbg___wbindgen_throw_83ebd457a191bc2a: function(arg0, arg1) {
31
+ throw new Error(getStringFromWasm0(arg0, arg1));
32
+ },
33
+ __wbg_new_2e5db3ea23dcc1a1: function() {
34
+ const ret = new Array();
35
+ return ret;
36
+ },
37
+ __wbg_new_5c365a7570baea64: function() {
38
+ const ret = new Object();
39
+ return ret;
40
+ },
41
+ __wbg_set_6be42768c690e380: function(arg0, arg1, arg2) {
42
+ arg0[arg1] = arg2;
43
+ },
44
+ __wbg_set_c7d56bae406212ae: function(arg0, arg1, arg2) {
45
+ arg0[arg1 >>> 0] = arg2;
46
+ },
47
+ __wbindgen_cast_0000000000000001: function(arg0, arg1) {
48
+ // Cast intrinsic for `Ref(String) -> Externref`.
49
+ const ret = getStringFromWasm0(arg0, arg1);
50
+ return ret;
51
+ },
52
+ __wbindgen_init_externref_table: function() {
53
+ const table = wasm.__wbindgen_externrefs;
54
+ const offset = table.grow(4);
55
+ table.set(0, undefined);
56
+ table.set(offset + 0, undefined);
57
+ table.set(offset + 1, null);
58
+ table.set(offset + 2, true);
59
+ table.set(offset + 3, false);
60
+ },
61
+ };
62
+ return {
63
+ __proto__: null,
64
+ "./sbpf_assembler_bg.js": import0,
65
+ };
66
+ }
67
+
68
+ function debugString(val) {
69
+ // primitive types
70
+ const type = typeof val;
71
+ if (type == 'number' || type == 'boolean' || val == null) {
72
+ return `${val}`;
73
+ }
74
+ if (type == 'string') {
75
+ return `"${val}"`;
76
+ }
77
+ if (type == 'symbol') {
78
+ const description = val.description;
79
+ if (description == null) {
80
+ return 'Symbol';
81
+ } else {
82
+ return `Symbol(${description})`;
83
+ }
84
+ }
85
+ if (type == 'function') {
86
+ const name = val.name;
87
+ if (typeof name == 'string' && name.length > 0) {
88
+ return `Function(${name})`;
89
+ } else {
90
+ return 'Function';
91
+ }
92
+ }
93
+ // objects
94
+ if (Array.isArray(val)) {
95
+ const length = val.length;
96
+ let debug = '[';
97
+ if (length > 0) {
98
+ debug += debugString(val[0]);
99
+ }
100
+ for(let i = 1; i < length; i++) {
101
+ debug += ', ' + debugString(val[i]);
102
+ }
103
+ debug += ']';
104
+ return debug;
105
+ }
106
+ // Test for built-in
107
+ const builtInMatches = /\[object ([^\]]+)\]/.exec(toString.call(val));
108
+ let className;
109
+ if (builtInMatches && builtInMatches.length > 1) {
110
+ className = builtInMatches[1];
111
+ } else {
112
+ // Failed to match the standard '[object ClassName]'
113
+ return toString.call(val);
114
+ }
115
+ if (className == 'Object') {
116
+ // we're a user defined class or Object
117
+ // JSON.stringify avoids problems with cycles, and is generally much
118
+ // easier than looping through ownProperties of `val`.
119
+ try {
120
+ return 'Object(' + JSON.stringify(val) + ')';
121
+ } catch (_) {
122
+ return 'Object';
123
+ }
124
+ }
125
+ // errors
126
+ if (val instanceof Error) {
127
+ return `${val.name}: ${val.message}\n${val.stack}`;
128
+ }
129
+ // TODO we could test for more things here, like `Set`s and `Map`s.
130
+ return className;
131
+ }
132
+
133
+ function getArrayU8FromWasm0(ptr, len) {
134
+ ptr = ptr >>> 0;
135
+ return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
136
+ }
137
+
138
+ let cachedDataViewMemory0 = null;
139
+ function getDataViewMemory0() {
140
+ if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
141
+ cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
142
+ }
143
+ return cachedDataViewMemory0;
144
+ }
145
+
146
+ function getStringFromWasm0(ptr, len) {
147
+ ptr = ptr >>> 0;
148
+ return decodeText(ptr, len);
149
+ }
150
+
151
+ let cachedUint8ArrayMemory0 = null;
152
+ function getUint8ArrayMemory0() {
153
+ if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
154
+ cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
155
+ }
156
+ return cachedUint8ArrayMemory0;
157
+ }
158
+
159
+ function passStringToWasm0(arg, malloc, realloc) {
160
+ if (realloc === undefined) {
161
+ const buf = cachedTextEncoder.encode(arg);
162
+ const ptr = malloc(buf.length, 1) >>> 0;
163
+ getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
164
+ WASM_VECTOR_LEN = buf.length;
165
+ return ptr;
166
+ }
167
+
168
+ let len = arg.length;
169
+ let ptr = malloc(len, 1) >>> 0;
170
+
171
+ const mem = getUint8ArrayMemory0();
172
+
173
+ let offset = 0;
174
+
175
+ for (; offset < len; offset++) {
176
+ const code = arg.charCodeAt(offset);
177
+ if (code > 0x7F) break;
178
+ mem[ptr + offset] = code;
179
+ }
180
+ if (offset !== len) {
181
+ if (offset !== 0) {
182
+ arg = arg.slice(offset);
183
+ }
184
+ ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
185
+ const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
186
+ const ret = cachedTextEncoder.encodeInto(arg, view);
187
+
188
+ offset += ret.written;
189
+ ptr = realloc(ptr, len, offset, 1) >>> 0;
190
+ }
191
+
192
+ WASM_VECTOR_LEN = offset;
193
+ return ptr;
194
+ }
195
+
196
+ function takeFromExternrefTable0(idx) {
197
+ const value = wasm.__wbindgen_externrefs.get(idx);
198
+ wasm.__externref_table_dealloc(idx);
199
+ return value;
200
+ }
201
+
202
+ let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
203
+ cachedTextDecoder.decode();
204
+ const MAX_SAFARI_DECODE_BYTES = 2146435072;
205
+ let numBytesDecoded = 0;
206
+ function decodeText(ptr, len) {
207
+ numBytesDecoded += len;
208
+ if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
209
+ cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
210
+ cachedTextDecoder.decode();
211
+ numBytesDecoded = len;
212
+ }
213
+ return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
214
+ }
215
+
216
+ const cachedTextEncoder = new TextEncoder();
217
+
218
+ if (!('encodeInto' in cachedTextEncoder)) {
219
+ cachedTextEncoder.encodeInto = function (arg, view) {
220
+ const buf = cachedTextEncoder.encode(arg);
221
+ view.set(buf);
222
+ return {
223
+ read: arg.length,
224
+ written: buf.length
225
+ };
226
+ };
227
+ }
228
+
229
+ let WASM_VECTOR_LEN = 0;
230
+
231
+ let wasmModule, wasm;
232
+ function __wbg_finalize_init(instance, module) {
233
+ wasm = instance.exports;
234
+ wasmModule = module;
235
+ cachedDataViewMemory0 = null;
236
+ cachedUint8ArrayMemory0 = null;
237
+ wasm.__wbindgen_start();
238
+ return wasm;
239
+ }
240
+
241
+ async function __wbg_load(module, imports) {
242
+ if (typeof Response === 'function' && module instanceof Response) {
243
+ if (typeof WebAssembly.instantiateStreaming === 'function') {
244
+ try {
245
+ return await WebAssembly.instantiateStreaming(module, imports);
246
+ } catch (e) {
247
+ const validResponse = module.ok && expectedResponseType(module.type);
248
+
249
+ if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {
250
+ console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
251
+
252
+ } else { throw e; }
253
+ }
254
+ }
255
+
256
+ const bytes = await module.arrayBuffer();
257
+ return await WebAssembly.instantiate(bytes, imports);
258
+ } else {
259
+ const instance = await WebAssembly.instantiate(module, imports);
260
+
261
+ if (instance instanceof WebAssembly.Instance) {
262
+ return { instance, module };
263
+ } else {
264
+ return instance;
265
+ }
266
+ }
267
+
268
+ function expectedResponseType(type) {
269
+ switch (type) {
270
+ case 'basic': case 'cors': case 'default': return true;
271
+ }
272
+ return false;
273
+ }
274
+ }
275
+
276
+ function initSync(module) {
277
+ if (wasm !== undefined) return wasm;
278
+
279
+
280
+ if (module !== undefined) {
281
+ if (Object.getPrototypeOf(module) === Object.prototype) {
282
+ ({module} = module)
283
+ } else {
284
+ console.warn('using deprecated parameters for `initSync()`; pass a single object instead')
285
+ }
286
+ }
287
+
288
+ const imports = __wbg_get_imports();
289
+ if (!(module instanceof WebAssembly.Module)) {
290
+ module = new WebAssembly.Module(module);
291
+ }
292
+ const instance = new WebAssembly.Instance(module, imports);
293
+ return __wbg_finalize_init(instance, module);
294
+ }
295
+
296
+ async function __wbg_init(module_or_path) {
297
+ if (wasm !== undefined) return wasm;
298
+
299
+
300
+ if (module_or_path !== undefined) {
301
+ if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
302
+ ({module_or_path} = module_or_path)
303
+ } else {
304
+ console.warn('using deprecated parameters for the initialization function; pass a single object instead')
305
+ }
306
+ }
307
+
308
+ if (module_or_path === undefined) {
309
+ module_or_path = new URL('sbpf_assembler_bg.wasm', import.meta.url);
310
+ }
311
+ const imports = __wbg_get_imports();
312
+
313
+ if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
314
+ module_or_path = fetch(module_or_path);
315
+ }
316
+
317
+ const { instance, module } = await __wbg_load(await module_or_path, imports);
318
+
319
+ return __wbg_finalize_init(instance, module);
320
+ }
321
+
322
+ export { initSync, __wbg_init as default };
Binary file
@@ -0,0 +1,10 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+ export const memory: WebAssembly.Memory;
4
+ export const assemble: (a: number, b: number, c: number) => [number, number, number, number];
5
+ export const __wbindgen_malloc: (a: number, b: number) => number;
6
+ export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
7
+ export const __wbindgen_externrefs: WebAssembly.Table;
8
+ export const __externref_table_dealloc: (a: number) => void;
9
+ export const __wbindgen_free: (a: number, b: number, c: number) => void;
10
+ export const __wbindgen_start: () => void;
package/package.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "@blueshift-gg/sbpf-assembler",
3
+ "description": "Assembler to assemble and link sbpf assembly to bytecode",
4
+ "version": "0.1.6",
5
+ "main": "dist/node/sbpf_assembler.js",
6
+ "browser": "dist/web/sbpf_assembler.js",
7
+ "types": "dist/node/sbpf_assembler.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "keywords": [
12
+ "sbpf",
13
+ "rust",
14
+ "wasm"
15
+ ],
16
+ "license": "MIT",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "https://github.com/blueshift-gg/sbpf.git"
20
+ }
21
+ }