aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2021-05-07 23:34:16 -0700
committerGravatar Jarred Sumner <jarred@jarredsumner.com> 2021-05-07 23:34:16 -0700
commite70ac2ce825ecf5f2d6aa16152801612bf23be8d (patch)
tree3909a1f32e12051389180034ee027c19656efabb
parent12615602144288b3123f352e5d586714bc01b4dc (diff)
downloadbun-e70ac2ce825ecf5f2d6aa16152801612bf23be8d.tar.gz
bun-e70ac2ce825ecf5f2d6aa16152801612bf23be8d.tar.zst
bun-e70ac2ce825ecf5f2d6aa16152801612bf23be8d.zip
wip
Former-commit-id: 79223472f7bb22c4de1f872a542f185aa7189aaa
-rw-r--r--src/api/demo/lib/api.ts65
-rw-r--r--src/api/demo/package.json5
-rw-r--r--src/api/demo/pnpm-lock.yaml131
-rw-r--r--src/api/schema.js4
-rw-r--r--src/api/schema.peechy2
-rw-r--r--src/api/schema.ts2
-rw-r--r--src/api/schema.zig1498
-rw-r--r--src/js_printer.zig5
-rw-r--r--src/main_wasm.zig81
-rw-r--r--src/string_mutable.zig57
-rw-r--r--src/zee_alloc.zig10
11 files changed, 986 insertions, 874 deletions
diff --git a/src/api/demo/lib/api.ts b/src/api/demo/lib/api.ts
index 9f07939c0..d2e19218e 100644
--- a/src/api/demo/lib/api.ts
+++ b/src/api/demo/lib/api.ts
@@ -1,5 +1,6 @@
import * as Schema from "../../schema";
import { ByteBuffer } from "peechy";
+import { transform as sucraseTransform } from "sucrase";
export interface WebAssemblyModule {
init(): number;
@@ -8,6 +9,7 @@ export interface WebAssemblyModule {
calloc(a: number): number;
realloc(a: number): number;
free(a: number): number;
+ cycle(): void;
}
const wasm_imports_sym: symbol | string =
@@ -19,6 +21,8 @@ const ptr_converter = new ArrayBuffer(8);
const ptr_float = new Float64Array(ptr_converter);
const slice = new Uint32Array(ptr_converter);
+var scratch: Uint8Array;
+
export class ESDev {
static has_initialized = false;
static wasm_source: WebAssembly.WebAssemblyInstantiatedSource = null;
@@ -114,41 +118,18 @@ export class ESDev {
};
static async init(url) {
+ globalThis.sucraseTransform = sucraseTransform;
+ scratch = new Uint8Array(8096);
+
if (ESDev.has_initialized) {
return;
}
- try {
- ESDev[wasm_imports_sym].memory = new WebAssembly.Memory({
- initial: 1500,
- // shared: typeof SharedArrayBuffer !== "undefined",
- maximum: typeof SharedArrayBuffer !== "undefined" ? 5000 : undefined,
- });
- } catch {
- try {
- ESDev[wasm_imports_sym].memory = new WebAssembly.Memory({
- initial: 750,
- // shared: typeof SharedArrayBuffer !== "undefined",
- maximum: typeof SharedArrayBuffer !== "undefined" ? 5000 : undefined,
- });
- } catch {
- try {
- ESDev[wasm_imports_sym].memory = new WebAssembly.Memory({
- initial: 375,
- // shared: typeof SharedArrayBuffer !== "undefined",
- maximum:
- typeof SharedArrayBuffer !== "undefined" ? 5000 : undefined,
- });
- } catch {
- ESDev[wasm_imports_sym].memory = new WebAssembly.Memory({
- initial: 125,
- // shared: typeof SharedArrayBuffer !== "undefined",
- maximum:
- typeof SharedArrayBuffer !== "undefined" ? 5000 : undefined,
- });
- }
- }
- }
+ ESDev[wasm_imports_sym].memory = new WebAssembly.Memory({
+ initial: 18,
+ // shared: typeof SharedArrayBuffer !== "undefined",
+ maximum: typeof SharedArrayBuffer !== "undefined" ? 5000 : undefined,
+ });
ESDev.wasm_source = await globalThis.WebAssembly.instantiateStreaming(
fetch(url),
@@ -166,18 +147,16 @@ export class ESDev {
ESDev.has_initialized = true;
}
- static transform(content: string, file_name: string) {
+ static transform(content: Uint8Array, file_name: string) {
if (!ESDev.has_initialized) {
throw "Please run await ESDev.init(wasm_url) before using this.";
}
- if (process.env.NODE_ENV === "development") {
- console.time("[ESDev] Transform " + file_name);
- }
+ // if (process.env.NODE_ENV === "development") {
+ // console.time("[ESDev] Transform " + file_name);
+ // }
- const bb = new ByteBuffer(
- new Uint8Array(content.length + file_name.length)
- );
+ const bb = new ByteBuffer(scratch);
bb.length = 0;
Schema.encodeTransform(
@@ -188,18 +167,16 @@ export class ESDev {
bb
);
const data = bb.toUint8Array();
+ if (bb._data.buffer !== scratch.buffer) {
+ scratch = bb._data;
+ }
const ptr = ESDev.wasm_exports.malloc(data.byteLength);
this._wasmPtrToSlice(ptr).set(data);
const resp_ptr = ESDev.wasm_exports.transform(ptr);
var _bb = new ByteBuffer(this._wasmPtrToSlice(resp_ptr));
const response = Schema.decodeTransformResponse(_bb);
- ESDev.wasm_exports.free(resp_ptr);
-
- if (process.env.NODE_ENV === "development") {
- console.timeEnd("[ESDev] Transform " + file_name);
- }
- ESDev.wasm_exports.free(resp_ptr);
+ ESDev.wasm_exports.cycle();
return response;
}
}
diff --git a/src/api/demo/package.json b/src/api/demo/package.json
index 588f0165f..c8caa7290 100644
--- a/src/api/demo/package.json
+++ b/src/api/demo/package.json
@@ -9,8 +9,9 @@
},
"dependencies": {
"next": "10.2.0",
- "peechy": "0.4.1",
+ "peechy": "0.4.3",
"react": "17.0.2",
- "react-dom": "17.0.2"
+ "react-dom": "17.0.2",
+ "sucrase": "^3.18.1"
}
}
diff --git a/src/api/demo/pnpm-lock.yaml b/src/api/demo/pnpm-lock.yaml
index 36eb423fa..bd5b33635 100644
--- a/src/api/demo/pnpm-lock.yaml
+++ b/src/api/demo/pnpm-lock.yaml
@@ -2,15 +2,17 @@ lockfileVersion: 5.3
specifiers:
next: 10.2.0
- peechy: 0.4.1
+ peechy: 0.4.3
react: 17.0.2
react-dom: 17.0.2
+ sucrase: ^3.18.1
dependencies:
next: 10.2.0_react-dom@17.0.2+react@17.0.2
- peechy: 0.4.1
+ peechy: 0.4.3
react: 17.0.2
react-dom: 17.0.2_react@17.0.2
+ sucrase: 3.18.1
packages:
@@ -143,6 +145,10 @@ packages:
color-convert: 2.0.1
dev: false
+ /any-promise/1.3.0:
+ resolution: {integrity: sha1-q8av7tzqUugJzcA3au0845Y10X8=}
+ dev: false
+
/anymatch/3.1.2:
resolution: {integrity: sha512-P43ePfOAIupkguHUycrc4qJ9kz8ZiuOUijaETwX7THt0Y/GNK7v0aa8rY816xWjZ7rJdA5XdMcpVFTKMq+RvWg==}
engines: {node: '>= 8'}
@@ -196,6 +202,10 @@ packages:
resolution: {integrity: sha1-CvMqmm4Tyno/1QaeYtew9Y0NiUY=}
dev: false
+ /balanced-match/1.0.2:
+ resolution: {integrity: sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==}
+ dev: false
+
/base64-js/1.5.1:
resolution: {integrity: sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==}
dev: false
@@ -217,6 +227,13 @@ packages:
resolution: {integrity: sha512-D7iWRBvnZE8ecXiLj/9wbxH7Tk79fAh8IHaTNq1RWRixsS02W+5qS+iE9yq6RYl0asXx5tw0bLhmT5pIfbSquw==}
dev: false
+ /brace-expansion/1.1.11:
+ resolution: {integrity: sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==}
+ dependencies:
+ balanced-match: 1.0.2
+ concat-map: 0.0.1
+ dev: false
+
/braces/3.0.2:
resolution: {integrity: sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==}
engines: {node: '>=8'}
@@ -434,10 +451,19 @@ packages:
resolution: {integrity: sha512-MKGMzyfeuutC/ZJ1cba9NqcNpfeqMUcYmyF1ZFY6/Cn7CNSAKx6a+s48sqLqyAiZuaP2TcqMhoo+dlwFnVxT9w==}
dev: false
+ /commander/4.1.1:
+ resolution: {integrity: sha512-NOKm8xhkzAjzFx8B2v5OAHT+u5pRQc2UCa2Vq9jYL/31o2wi9mxBA7LIFs3sV5VSC49z6pEhfbMULvShKj26WA==}
+ engines: {node: '>= 6'}
+ dev: false
+
/commondir/1.0.1:
resolution: {integrity: sha1-3dgA2gxmEnOTzKWVDqloo6rxJTs=}
dev: false
+ /concat-map/0.0.1:
+ resolution: {integrity: sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=}
+ dev: false
+
/console-browserify/1.2.0:
resolution: {integrity: sha512-ZMkYO/LkF17QvCPqM0gxw8yUzigAOZOSWSHg91FH6orS7vcEj5dVZTidN2fQ14yBSdg97RqhSNwLUXInd52OTA==}
dev: false
@@ -707,6 +733,10 @@ packages:
resolution: {integrity: sha1-C+4AUBiusmDQo6865ljdATbsG5k=}
dev: false
+ /fs.realpath/1.0.0:
+ resolution: {integrity: sha1-FQStJSMVjKpA20onh8sBQRmU6k8=}
+ dev: false
+
/fsevents/2.3.2:
resolution: {integrity: sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==}
engines: {node: ^8.16.0 || ^10.6.0 || >=11.0.0}
@@ -743,6 +773,17 @@ packages:
resolution: {integrity: sha512-lkX1HJXwyMcprw/5YUZc2s7DrpAiHB21/V+E1rHUrVNokkvB6bqMzT0VfV6/86ZNabt1k14YOIaT7nDvOX3Iiw==}
dev: false
+ /glob/7.1.6:
+ resolution: {integrity: sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA==}
+ dependencies:
+ fs.realpath: 1.0.0
+ inflight: 1.0.6
+ inherits: 2.0.4
+ minimatch: 3.0.4
+ once: 1.4.0
+ path-is-absolute: 1.0.1
+ dev: false
+
/graceful-fs/4.2.6:
resolution: {integrity: sha512-nTnJ528pbqxYanhpDYsi4Rd8MAeaBA67+RZ10CM1m3bTAVFEDcd5AuA4a6W5YkGZ1iNXHzZz8T6TBKLeBuNriQ==}
dev: false
@@ -842,6 +883,13 @@ packages:
resolution: {integrity: sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==}
dev: false
+ /inflight/1.0.6:
+ resolution: {integrity: sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=}
+ dependencies:
+ once: 1.4.0
+ wrappy: 1.0.2
+ dev: false
+
/inherits/2.0.1:
resolution: {integrity: sha1-sX0I0ya0Qj5Wjv9xn5GwscvfafE=}
dev: false
@@ -984,6 +1032,10 @@ packages:
minimist: 1.2.5
dev: false
+ /lines-and-columns/1.1.6:
+ resolution: {integrity: sha1-HADHQ7QzzQpOgHWPe2SldEDZ/wA=}
+ dev: false
+
/loader-utils/1.2.3:
resolution: {integrity: sha512-fkpz8ejdnEMG3s37wGL07iSBDg99O9D5yflE9RGNH3hRdx9SOwYfnGYdZOUIZitN8E+E2vkq3MUMYMvPYl5ZZA==}
engines: {node: '>=4.0.0'}
@@ -1056,6 +1108,12 @@ packages:
resolution: {integrity: sha1-9sAMHAsIIkblxNmd+4x8CDsrWCo=}
dev: false
+ /minimatch/3.0.4:
+ resolution: {integrity: sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==}
+ dependencies:
+ brace-expansion: 1.1.11
+ dev: false
+
/minimist/1.2.5:
resolution: {integrity: sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==}
dev: false
@@ -1064,6 +1122,14 @@ packages:
resolution: {integrity: sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=}
dev: false
+ /mz/2.7.0:
+ resolution: {integrity: sha512-z81GNO7nnYMEhrGh9LeymoE4+Yr0Wn5McHIZMK5cfQCl+NDX08sCZgUc9/6MHni9IWuFLm1Z3HTCXu2z9fN62Q==}
+ dependencies:
+ any-promise: 1.3.0
+ object-assign: 4.1.1
+ thenify-all: 1.6.0
+ dev: false
+
/nanoid/3.1.22:
resolution: {integrity: sha512-/2ZUaJX2ANuLtTvqTlgqBQNJoQO398KyJgZloL0PZkC0dpysjncRUPsFe3DUPzz/y3h+u7C46np8RMuvF3jsSQ==}
engines: {node: ^10 || ^12 || ^13.7 || ^14 || >=15.0.1}
@@ -1197,6 +1263,11 @@ packages:
vm-browserify: 1.1.2
dev: false
+ /node-modules-regexp/1.0.0:
+ resolution: {integrity: sha1-jZ2+KJZKSsVxLpExZCEHxx6Q7EA=}
+ engines: {node: '>=0.10.0'}
+ dev: false
+
/node-releases/1.1.71:
resolution: {integrity: sha512-zR6HoT6LrLCRBwukmrVbHv0EpEQjksO6GmFcZQQuCAy139BEsoVKPYnf3jongYW83fAa1torLGYwxxky/p28sg==}
dev: false
@@ -1238,6 +1309,12 @@ packages:
object-keys: 1.1.1
dev: false
+ /once/1.4.0:
+ resolution: {integrity: sha1-WDsap3WWHUsROsF9nFC6753Xa9E=}
+ dependencies:
+ wrappy: 1.0.2
+ dev: false
+
/os-browserify/0.3.0:
resolution: {integrity: sha1-hUNzx/XCMVkU/Jv8a9gjj92h7Cc=}
dev: false
@@ -1316,6 +1393,11 @@ packages:
engines: {node: '>=8'}
dev: false
+ /path-is-absolute/1.0.1:
+ resolution: {integrity: sha1-F0uSaHNVNP+8es5r9TpanhtcX18=}
+ engines: {node: '>=0.10.0'}
+ dev: false
+
/pbkdf2/3.1.2:
resolution: {integrity: sha512-iuh7L6jA7JEGu2WxDwtQP1ddOpaJNC4KlDEFfdQajSGgGPNi4OyDc2R7QnbY2bR9QjBVGwgvTdNJZoE7RaxUMA==}
engines: {node: '>=0.12'}
@@ -1327,8 +1409,8 @@ packages:
sha.js: 2.4.11
dev: false
- /peechy/0.4.1:
- resolution: {integrity: sha512-IWelrwiGJRd3EmIvF0DcDQdJldy2QzPXY5fU42AAkqCQoMo+mX7lQIilMwLmPX3RjPfQY9Bv3pK9K8iSi2HlWA==}
+ /peechy/0.4.3:
+ resolution: {integrity: sha512-V7caZQjhB1KYuvyLgUEtzyTT0xEfU5X+kK26fKoQVGuS5PpuBtJxnI1UcVW1dvxpfYJ/D+mCjUnq+bVCvZSSsw==}
hasBin: true
dependencies:
change-case: 4.1.2
@@ -1339,6 +1421,13 @@ packages:
engines: {node: '>=8.6'}
dev: false
+ /pirates/4.0.1:
+ resolution: {integrity: sha512-WuNqLTbMI3tmfef2TKxlQmAiLHKtFhlsCZnPIpuv2Ow0RDVO8lfy1Opf4NUzlMXLjPl+Men7AuVdX6TA+s+uGA==}
+ engines: {node: '>= 6'}
+ dependencies:
+ node-modules-regexp: 1.0.0
+ dev: false
+
/pkg-dir/4.2.0:
resolution: {integrity: sha512-HRDzbaKjC+AOWVXxAU/x54COGeIv9eb+6CkDSQoNTt4XyWoIJvuPsXizxu/Fr23EiekbtZwmh1IcIG/l/a10GQ==}
engines: {node: '>=8'}
@@ -1706,6 +1795,19 @@ packages:
resolution: {integrity: sha512-8/3pSmthWM7lsPBKv7NXkzn2Uc9W7NotcwGNpJaa3k7WMM1XDCA4MgT5k/8BIexd5ydZdboXtU90XH9Ec4Bv/Q==}
dev: false
+ /sucrase/3.18.1:
+ resolution: {integrity: sha512-TRyO38wwOPhLLlM8QLOG3TgMj0FKk+arlTrS9pRAanF8cAcHvgRPKIYWGO25mPSp/Rj87zMMTjFfkqIZGI6ZdA==}
+ engines: {node: '>=8'}
+ hasBin: true
+ dependencies:
+ commander: 4.1.1
+ glob: 7.1.6
+ lines-and-columns: 1.1.6
+ mz: 2.7.0
+ pirates: 4.0.1
+ ts-interface-checker: 0.1.13
+ dev: false
+
/supports-color/5.5.0:
resolution: {integrity: sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==}
engines: {node: '>=4'}
@@ -1727,6 +1829,19 @@ packages:
has-flag: 4.0.0
dev: false
+ /thenify-all/1.6.0:
+ resolution: {integrity: sha1-GhkY1ALY/D+Y+/I02wvMjMEOlyY=}
+ engines: {node: '>=0.8'}
+ dependencies:
+ thenify: 3.3.1
+ dev: false
+
+ /thenify/3.3.1:
+ resolution: {integrity: sha512-RVZSIV5IG10Hk3enotrhvz0T9em6cyHBLkH/YAZuKqd8hRkKhSfCGIcP2KUY0EPxndzANBmNllzWPwak+bheSw==}
+ dependencies:
+ any-promise: 1.3.0
+ dev: false
+
/timers-browserify/2.0.12:
resolution: {integrity: sha512-9phl76Cqm6FhSX9Xe1ZUAMLtm1BLkKj2Qd5ApyWkXzsMRaA7dgr81kf4wJmQf/hAvg8EEyJxDo3du/0KlhPiKQ==}
engines: {node: '>=0.6.0'}
@@ -1761,6 +1876,10 @@ packages:
punycode: 2.1.1
dev: false
+ /ts-interface-checker/0.1.13:
+ resolution: {integrity: sha512-Y/arvbn+rrz3JCKl9C4kVNfTfSm2/mEp5FSz5EsZSANGPSlQrpRI5M4PKF+mJnE52jOO90PnPSc3Ur3bTQw0gA==}
+ dev: false
+
/ts-pnp/1.2.0:
resolution: {integrity: sha512-csd+vJOb/gkzvcCHgTGSChYpy5f1/XKNsmvBGO4JXS+z1v2HobugDz4s1IeFXM3wZB44uczs+eazB5Q/ccdhQw==}
engines: {node: '>=6'}
@@ -1904,6 +2023,10 @@ packages:
is-typed-array: 1.1.5
dev: false
+ /wrappy/1.0.2:
+ resolution: {integrity: sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=}
+ dev: false
+
/xtend/4.0.2:
resolution: {integrity: sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==}
engines: {node: '>=0.4'}
diff --git a/src/api/schema.js b/src/api/schema.js
index 2a695a33c..484ced1df 100644
--- a/src/api/schema.js
+++ b/src/api/schema.js
@@ -250,7 +250,7 @@ function decodeTransform(bb) {
break;
case 3:
- result["contents"] = bb.readString();
+ result["contents"] = bb.readByteArray();
break;
case 4:
@@ -284,7 +284,7 @@ function encodeTransform(message, bb) {
var value = message["contents"];
if (value != null) {
bb.writeByte(3);
- bb.writeString(value);
+ bb.writeByteArray(value);
}
var value = message["loader"];
diff --git a/src/api/schema.peechy b/src/api/schema.peechy
index 2f4579201..1a5a8cc00 100644
--- a/src/api/schema.peechy
+++ b/src/api/schema.peechy
@@ -50,7 +50,7 @@ struct FileHandle {
message Transform {
FileHandle handle = 1;
string path = 2;
- string contents = 3;
+ byte[] contents = 3;
Loader loader = 4;
TransformOptions options = 5;
diff --git a/src/api/schema.ts b/src/api/schema.ts
index a922b6a33..344e87604 100644
--- a/src/api/schema.ts
+++ b/src/api/schema.ts
@@ -101,7 +101,7 @@ type uint32 = number;
export interface Transform {
handle?: FileHandle;
path?: string;
- contents?: string;
+ contents?: Uint8Array;
loader?: Loader;
options?: TransformOptions;
}
diff --git a/src/api/schema.zig b/src/api/schema.zig
index c10e5b5a3..5931158df 100644
--- a/src/api/schema.zig
+++ b/src/api/schema.zig
@@ -1,782 +1,744 @@
-
const std = @import("std");
-
-pub const Api = struct {
-
-pub const Loader = enum(u8) {
-
-_none,
- /// jsx
- jsx,
-
- /// js
- js,
-
- /// ts
- ts,
-
- /// tsx
- tsx,
-
- /// css
- css,
-
- /// file
- file,
-
- /// json
- json,
-
-_,
-
- pub fn jsonStringify(self: *const @This(), opts: anytype, o: anytype) !void {
- return try std.json.stringify(@tagName(self), opts, o);
+pub const Api = struct {
+ pub const Loader = enum(u8) {
+ _none,
+ /// jsx
+ jsx,
+
+ /// js
+ js,
+
+ /// ts
+ ts,
+
+ /// tsx
+ tsx,
+
+ /// css
+ css,
+
+ /// file
+ file,
+
+ /// json
+ json,
+
+ _,
+
+ pub fn jsonStringify(self: *const @This(), opts: anytype, o: anytype) !void {
+ return try std.json.stringify(@tagName(self), opts, o);
+ }
+ };
+
+ pub const JsxRuntime = enum(u8) {
+ _none,
+ /// automatic
+ automatic,
+
+ /// classic
+ classic,
+
+ _,
+
+ pub fn jsonStringify(self: *const @This(), opts: anytype, o: anytype) !void {
+ return try std.json.stringify(@tagName(self), opts, o);
+ }
+ };
+
+ pub const Jsx = struct {
+ /// factory
+ factory: []u8,
+
+ /// runtime
+ runtime: JsxRuntime,
+
+ /// fragment
+ fragment: []u8,
+
+ /// production
+ production: bool = false,
+
+ /// import_source
+ import_source: []u8,
+
+ /// react_fast_refresh
+ react_fast_refresh: bool = false,
+
+ /// loader_keys
+ loader_keys: [][]u8,
+
+ /// loader_values
+ loader_values: []Loader,
+
+ pub fn decode(allocator: *std.mem.Allocator, reader: anytype) anyerror!Jsx {
+ var obj = std.mem.zeroes(Jsx);
+ try update(&obj, allocator, reader);
+ return obj;
+ }
+ pub fn update(result: *Jsx, allocator: *std.mem.Allocator, reader: anytype) anyerror!void {
+ var length: usize = 0;
+ length = try reader.readIntNative(u32);
+ if (result.factory.len != length) {
+ result.factory = try allocator.alloc(u8, length);
+ }
+ _ = try reader.readAll(result.factory);
+ result.runtime = try reader.readEnum(JsxRuntime, .Little);
+ length = try reader.readIntNative(u32);
+ if (result.fragment.len != length) {
+ result.fragment = try allocator.alloc(u8, length);
+ }
+ _ = try reader.readAll(result.fragment);
+ result.production = (try reader.readByte()) == @as(u8, 1);
+ length = try reader.readIntNative(u32);
+ if (result.import_source.len != length) {
+ result.import_source = try allocator.alloc(u8, length);
+ }
+ _ = try reader.readAll(result.import_source);
+ result.react_fast_refresh = (try reader.readByte()) == @as(u8, 1);
+ {
+ var array_count = try reader.readIntNative(u32);
+ if (array_count != result.loader_keys.len) {
+ result.loader_keys = try allocator.alloc([]u8, array_count);
}
-
-
-};
-
-pub const JsxRuntime = enum(u8) {
-
-_none,
- /// automatic
- automatic,
-
- /// classic
- classic,
-
-_,
-
- pub fn jsonStringify(self: *const @This(), opts: anytype, o: anytype) !void {
- return try std.json.stringify(@tagName(self), opts, o);
+ length = try reader.readIntNative(u32);
+ for (result.loader_keys) |content, j| {
+ if (result.loader_keys[j].len != length and length > 0) {
+ result.loader_keys[j] = try allocator.alloc(u8, length);
+ }
+ _ = try reader.readAll(result.loader_keys[j]);
}
-
-
-};
-
-pub const Jsx = struct {
-/// factory
-factory: []u8,
-
-/// runtime
-runtime: JsxRuntime,
-
-/// fragment
-fragment: []u8,
-
-/// production
-production: bool = false,
-
-/// import_source
-import_source: []u8,
-
-/// react_fast_refresh
-react_fast_refresh: bool = false,
-
-/// loader_keys
-loader_keys: [][]u8,
-
-/// loader_values
-loader_values: []Loader,
-
-
-pub fn decode(allocator: *std.mem.Allocator, reader: anytype) anyerror!Jsx {
-var obj = std.mem.zeroes(Jsx);
-try update(&obj, allocator, reader);
-return obj;
-}
-pub fn update(result: *Jsx, allocator: *std.mem.Allocator, reader: anytype) anyerror!void {
-
- var length: usize = 0;
- length = try reader.readIntNative(u32);
- if (result.factory.len != length) {
- result.factory = try allocator.alloc(u8, length);
- }
- _ = try reader.readAll(result.factory);
- result.runtime = try reader.readEnum(JsxRuntime, .Little);
- length = try reader.readIntNative(u32);
- if (result.fragment.len != length) {
- result.fragment = try allocator.alloc(u8, length);
- }
- _ = try reader.readAll(result.fragment);
- result.production = (try reader.readByte()) == @as(u8, 1);
- length = try reader.readIntNative(u32);
- if (result.import_source.len != length) {
- result.import_source = try allocator.alloc(u8, length);
- }
- _ = try reader.readAll(result.import_source);
- result.react_fast_refresh = (try reader.readByte()) == @as(u8, 1);
- {
- var array_count = try reader.readIntNative(u32);
- if (array_count != result.loader_keys.len) {
- result.loader_keys = try allocator.alloc([]u8, array_count);
- }
- length = try reader.readIntNative(u32);
- for (result.loader_keys) |content, j| {
- if (result.loader_keys[j].len != length) {
- result.loader_keys[j] = try allocator.alloc(u8, length);
- }
- _ = try reader.readAll(result.loader_keys[j]);
- }
- }
- length = try reader.readIntNative(u32);
- result.loader_values = try allocator.alloc(Loader, length);
- {
- var j: usize = 0;
- while(j < length) : (j += 1) {
- result.loader_values[j] = try reader.readEnum(Loader, .Little);
- }}
- return;
-}
-
-pub fn encode(result: *const @This(), writer: anytype) anyerror!void {
-
- var n: usize = 0;
- try writer.writeIntNative(u32, @intCast(u32, result.factory.len));
- try writer.writeAll(std.mem.sliceAsBytes(result.factory));
-
- try writer.writeIntNative(@TypeOf(@enumToInt(result.runtime)), @enumToInt(result.runtime));
-
- try writer.writeIntNative(u32, @intCast(u32, result.fragment.len));
- try writer.writeAll(std.mem.sliceAsBytes(result.fragment));
-
- try writer.writeByte(@boolToInt(result.production));
-
- try writer.writeIntNative(u32, @intCast(u32, result.import_source.len));
- try writer.writeAll(std.mem.sliceAsBytes(result.import_source));
-
- try writer.writeByte(@boolToInt(result.react_fast_refresh));
-
- n = result.loader_keys.len;
- _ = try writer.writeIntNative(u32, @intCast(u32, n));
- {
- var j: usize = 0;
- while (j < n) : (j += 1) {
- _ = try writer.writeIntNative(u32, @intCast(u32, result.loader_keys[j].len));
- try writer.writeAll(std.mem.sliceAsBytes(result.loader_keys[j]));
- }}
-
- n = result.loader_values.len;
- _ = try writer.writeIntNative(u32, @intCast(u32, n));
- {
- var j: usize = 0;
- while (j < n) : (j += 1) {
- try writer.writeByte(@enumToInt(result.loader_values[j]));
- }}
- return;
-}
-
-};
-
-pub const TransformOptions = struct {
-/// jsx
-jsx: Jsx,
-
-/// ts
-ts: bool = false,
-
-/// base_path
-base_path: []u8,
-
-/// define_keys
-define_keys: [][]u8,
-
-/// define_values
-define_values: [][]u8,
-
-
-pub fn decode(allocator: *std.mem.Allocator, reader: anytype) anyerror!TransformOptions {
-var obj = std.mem.zeroes(TransformOptions);
-try update(&obj, allocator, reader);
-return obj;
-}
-pub fn update(result: *TransformOptions, allocator: *std.mem.Allocator, reader: anytype) anyerror!void {
-
- var length: usize = 0;
- result.jsx = try Jsx.decode(allocator, reader);
- result.ts = (try reader.readByte()) == @as(u8, 1);
- length = try reader.readIntNative(u32);
- if (result.base_path.len != length) {
- result.base_path = try allocator.alloc(u8, length);
- }
- _ = try reader.readAll(result.base_path);
- {
- var array_count = try reader.readIntNative(u32);
- if (array_count != result.define_keys.len) {
- result.define_keys = try allocator.alloc([]u8, array_count);
- }
- length = try reader.readIntNative(u32);
- for (result.define_keys) |content, j| {
- if (result.define_keys[j].len != length) {
- result.define_keys[j] = try allocator.alloc(u8, length);
- }
- _ = try reader.readAll(result.define_keys[j]);
- }
- }
- {
- var array_count = try reader.readIntNative(u32);
- if (array_count != result.define_values.len) {
- result.define_values = try allocator.alloc([]u8, array_count);
- }
- length = try reader.readIntNative(u32);
- for (result.define_values) |content, j| {
- if (result.define_values[j].len != length) {
- result.define_values[j] = try allocator.alloc(u8, length);
- }
- _ = try reader.readAll(result.define_values[j]);
- }
- }
- return;
-}
-
-pub fn encode(result: *const @This(), writer: anytype) anyerror!void {
-
- var n: usize = 0;
- try result.jsx.encode(writer);
-
- try writer.writeByte(@boolToInt(result.ts));
-
- try writer.writeIntNative(u32, @intCast(u32, result.base_path.len));
- try writer.writeAll(std.mem.sliceAsBytes(result.base_path));
-
- n = result.define_keys.len;
- _ = try writer.writeIntNative(u32, @intCast(u32, n));
- {
- var j: usize = 0;
- while (j < n) : (j += 1) {
- _ = try writer.writeIntNative(u32, @intCast(u32, result.define_keys[j].len));
- try writer.writeAll(std.mem.sliceAsBytes(result.define_keys[j]));
- }}
-
- n = result.define_values.len;
- _ = try writer.writeIntNative(u32, @intCast(u32, n));
- {
- var j: usize = 0;
- while (j < n) : (j += 1) {
- _ = try writer.writeIntNative(u32, @intCast(u32, result.define_values[j].len));
- try writer.writeAll(std.mem.sliceAsBytes(result.define_values[j]));
- }}
- return;
-}
-
-};
-
-pub const FileHandle = struct {
-/// path
-path: []u8,
-
-/// size
-size: u32 = 0,
-
-/// fd
-fd: u32 = 0,
-
-
-pub fn decode(allocator: *std.mem.Allocator, reader: anytype) anyerror!FileHandle {
-var obj = std.mem.zeroes(FileHandle);
-try update(&obj, allocator, reader);
-return obj;
-}
-pub fn update(result: *FileHandle, allocator: *std.mem.Allocator, reader: anytype) anyerror!void {
-
- var length: usize = 0;
- length = try reader.readIntNative(u32);
- if (result.path.len != length) {
- result.path = try allocator.alloc(u8, length);
- }
- _ = try reader.readAll(result.path);
- _ = try reader.readAll(std.mem.asBytes(&result.size));
- _ = try reader.readAll(std.mem.asBytes(&result.fd));
- return;
-}
-
-pub fn encode(result: *const @This(), writer: anytype) anyerror!void {
-
- try writer.writeIntNative(u32, @intCast(u32, result.path.len));
- try writer.writeAll(std.mem.sliceAsBytes(result.path));
-
- try writer.writeIntNative(u32, result.size);
-
- try writer.writeIntNative(u32, result.fd);
- return;
-}
-
-};
-
-pub const Transform = struct {
-/// handle
-handle: ?FileHandle = null,
-
-/// path
-path: ?[]u8 = null,
-
-/// contents
-contents: ?[]u8 = null,
-
-/// loader
-loader: ?Loader = null,
-
-/// options
-options: ?TransformOptions = null,
-
-
-pub fn decode(allocator: *std.mem.Allocator, reader: anytype) anyerror!Transform {
-var obj = std.mem.zeroes(Transform);
-try update(&obj, allocator, reader);
-return obj;
-}
-pub fn update(result: *Transform, allocator: *std.mem.Allocator, reader: anytype) anyerror!void {
-
- var length: usize = 0;
- while(true) {
- const field_type: u8 = try reader.readByte();
- switch (field_type) {
- 0 => { return; },
-
- 1 => {
- result.handle = try FileHandle.decode(allocator, reader);
-},
- 2 => {
- length = try reader.readIntNative(u32);
- if ((result.path orelse &([_]u8{})).len != length) {
- result.path = try allocator.alloc(u8, length);
- }
- _ = try reader.readAll(result.path.?);
-},
- 3 => {
- length = try reader.readIntNative(u32);
- if ((result.contents orelse &([_]u8{})).len != length) {
- result.contents = try allocator.alloc(u8, length);
- }
- _ = try reader.readAll(result.contents.?);
-},
- 4 => {
- result.loader = try reader.readEnum(Loader, .Little);
-},
- 5 => {
- result.options = try TransformOptions.decode(allocator, reader);
-},
- else => {
- return error.InvalidMessage;
- }
- }}
-}
-
-pub fn encode(result: *const @This(), writer: anytype) anyerror!void {
-
- if (result.handle) |handle| {
- try writer.writeByte(1);
- try handle.encode(writer);
- }
-
- if (result.path) |path| {
- try writer.writeByte(2);
- try writer.writeIntNative(u32, @intCast(u32, path.len));
- try writer.writeAll(std.mem.sliceAsBytes(path));
- }
-
- if (result.contents) |contents| {
- try writer.writeByte(3);
- try writer.writeIntNative(u32, @intCast(u32, contents.len));
- try writer.writeAll(std.mem.sliceAsBytes(contents));
- }
-
- if (result.loader) |loader| {
- try writer.writeByte(4);
- try writer.writeIntNative(@TypeOf(@enumToInt(result.loader orelse unreachable)), @enumToInt(result.loader orelse unreachable));
- }
-
- if (result.options) |options| {
- try writer.writeByte(5);
- try options.encode(writer);
- }
- try writer.writeByte(0);
- return;
-}
-
-};
-
-pub const TransformResponseStatus = enum(u32) {
-
-_none,
- /// success
- success,
-
- /// fail
- fail,
-
-_,
-
- pub fn jsonStringify(self: *const @This(), opts: anytype, o: anytype) !void {
- return try std.json.stringify(@tagName(self), opts, o);
+ }
+ length = try reader.readIntNative(u32);
+ result.loader_values = try allocator.alloc(Loader, length);
+ {
+ var j: usize = 0;
+ while (j < length) : (j += 1) {
+ result.loader_values[j] = try reader.readEnum(Loader, .Little);
}
+ }
+ return;
+ }
-
-};
-
-pub const OutputFile = struct {
-/// data
-data: []u8,
-
-/// path
-path: []u8,
-
-
-pub fn decode(allocator: *std.mem.Allocator, reader: anytype) anyerror!OutputFile {
-var obj = std.mem.zeroes(OutputFile);
-try update(&obj, allocator, reader);
-return obj;
-}
-pub fn update(result: *OutputFile, allocator: *std.mem.Allocator, reader: anytype) anyerror!void {
-
- var length: usize = 0;
- _ = try reader.readAll(result.data);
- length = try reader.readIntNative(u32);
- if (result.path.len != length) {
- result.path = try allocator.alloc(u8, length);
- }
- _ = try reader.readAll(result.path);
- return;
-}
-
-pub fn encode(result: *const @This(), writer: anytype) anyerror!void {
-
- try writer.writeIntNative(u32, @intCast(u32, result.data.len));
- try writer.writeAll(result.data);
-
- try writer.writeIntNative(u32, @intCast(u32, result.path.len));
- try writer.writeAll(std.mem.sliceAsBytes(result.path));
- return;
-}
-
-};
-
-pub const TransformResponse = struct {
-/// status
-status: TransformResponseStatus,
-
-/// files
-files: []OutputFile,
-
-/// errors
-errors: []Message,
-
-
-pub fn decode(allocator: *std.mem.Allocator, reader: anytype) anyerror!TransformResponse {
-var obj = std.mem.zeroes(TransformResponse);
-try update(&obj, allocator, reader);
-return obj;
-}
-pub fn update(result: *TransformResponse, allocator: *std.mem.Allocator, reader: anytype) anyerror!void {
-
- var length: usize = 0;
- result.status = try reader.readEnum(TransformResponseStatus, .Little);
- length = try reader.readIntNative(u32);
- result.files = try allocator.alloc(OutputFile, length);
- {
- var j: usize = 0;
- while(j < length) : (j += 1) {
- result.files[j] = try OutputFile.decode(allocator, reader);
- }}
- length = try reader.readIntNative(u32);
- result.errors = try allocator.alloc(Message, length);
- {
- var j: usize = 0;
- while(j < length) : (j += 1) {
- result.errors[j] = try Message.decode(allocator, reader);
- }}
- return;
-}
-
-pub fn encode(result: *const @This(), writer: anytype) anyerror!void {
-
- var n: usize = 0;
- try writer.writeIntNative(@TypeOf(@enumToInt(result.status)), @enumToInt(result.status));
-
- n = result.files.len;
- _ = try writer.writeIntNative(u32, @intCast(u32, n));
- {
- var j: usize = 0;
- while (j < n) : (j += 1) {
- try result.files[j].encode(writer);
-
- }}
-
- n = result.errors.len;
- _ = try writer.writeIntNative(u32, @intCast(u32, n));
- {
- var j: usize = 0;
- while (j < n) : (j += 1) {
- try result.errors[j].encode(writer);
-
- }}
- return;
-}
+ pub fn encode(result: *const @This(), writer: anytype) anyerror!void {
+ var n: usize = 0;
+ try writer.writeIntNative(u32, @intCast(u32, result.factory.len));
+ try writer.writeAll(std.mem.sliceAsBytes(result.factory));
-};
-
-pub const MessageKind = enum(u32) {
-
-_none,
- /// err
- err,
+ try writer.writeIntNative(@TypeOf(@enumToInt(result.runtime)), @enumToInt(result.runtime));
- /// warn
- warn,
+ try writer.writeIntNative(u32, @intCast(u32, result.fragment.len));
+ try writer.writeAll(std.mem.sliceAsBytes(result.fragment));
- /// note
- note,
+ try writer.writeByte(@boolToInt(result.production));
- /// debug
- debug,
+ try writer.writeIntNative(u32, @intCast(u32, result.import_source.len));
+ try writer.writeAll(std.mem.sliceAsBytes(result.import_source));
-_,
+ try writer.writeByte(@boolToInt(result.react_fast_refresh));
- pub fn jsonStringify(self: *const @This(), opts: anytype, o: anytype) !void {
- return try std.json.stringify(@tagName(self), opts, o);
+ n = result.loader_keys.len;
+ _ = try writer.writeIntNative(u32, @intCast(u32, n));
+ {
+ var j: usize = 0;
+ while (j < n) : (j += 1) {
+ _ = try writer.writeIntNative(u32, @intCast(u32, result.loader_keys[j].len));
+ try writer.writeAll(std.mem.sliceAsBytes(result.loader_keys[j]));
}
-
-
-};
-
-pub const Location = struct {
-/// file
-file: []u8,
-
-/// namespace
-namespace: []u8,
-
-/// line
-line: i32 = 0,
-
-/// column
-column: i32 = 0,
-
-/// line_text
-line_text: []u8,
-
-/// suggestion
-suggestion: []u8,
-
-/// offset
-offset: u32 = 0,
-
-
-pub fn decode(allocator: *std.mem.Allocator, reader: anytype) anyerror!Location {
-var obj = std.mem.zeroes(Location);
-try update(&obj, allocator, reader);
-return obj;
-}
-pub fn update(result: *Location, allocator: *std.mem.Allocator, reader: anytype) anyerror!void {
-
- var length: usize = 0;
- length = try reader.readIntNative(u32);
- if (result.file.len != length) {
- result.file = try allocator.alloc(u8, length);
- }
- _ = try reader.readAll(result.file);
- length = try reader.readIntNative(u32);
- if (result.namespace.len != length) {
- result.namespace = try allocator.alloc(u8, length);
- }
- _ = try reader.readAll(result.namespace);
- _ = try reader.readAll(std.mem.asBytes(&result.line));
- _ = try reader.readAll(std.mem.asBytes(&result.column));
- length = try reader.readIntNative(u32);
- if (result.line_text.len != length) {
- result.line_text = try allocator.alloc(u8, length);
- }
- _ = try reader.readAll(result.line_text);
- length = try reader.readIntNative(u32);
- if (result.suggestion.len != length) {
- result.suggestion = try allocator.alloc(u8, length);
- }
- _ = try reader.readAll(result.suggestion);
- _ = try reader.readAll(std.mem.asBytes(&result.offset));
- return;
-}
-
-pub fn encode(result: *const @This(), writer: anytype) anyerror!void {
-
- try writer.writeIntNative(u32, @intCast(u32, result.file.len));
- try writer.writeAll(std.mem.sliceAsBytes(result.file));
-
- try writer.writeIntNative(u32, @intCast(u32, result.namespace.len));
- try writer.writeAll(std.mem.sliceAsBytes(result.namespace));
-
- try writer.writeIntNative(i32, result.line);
-
- try writer.writeIntNative(i32, result.column);
-
- try writer.writeIntNative(u32, @intCast(u32, result.line_text.len));
- try writer.writeAll(std.mem.sliceAsBytes(result.line_text));
-
- try writer.writeIntNative(u32, @intCast(u32, result.suggestion.len));
- try writer.writeAll(std.mem.sliceAsBytes(result.suggestion));
-
- try writer.writeIntNative(u32, result.offset);
- return;
-}
-
-};
-
-pub const MessageData = struct {
-/// text
-text: ?[]u8 = null,
-
-/// location
-location: ?Location = null,
-
-
-pub fn decode(allocator: *std.mem.Allocator, reader: anytype) anyerror!MessageData {
-var obj = std.mem.zeroes(MessageData);
-try update(&obj, allocator, reader);
-return obj;
-}
-pub fn update(result: *MessageData, allocator: *std.mem.Allocator, reader: anytype) anyerror!void {
-
- var length: usize = 0;
- while(true) {
- const field_type: u8 = try reader.readByte();
- switch (field_type) {
- 0 => { return; },
-
- 1 => {
- length = try reader.readIntNative(u32);
- if ((result.text orelse &([_]u8{})).len != length) {
- result.text = try allocator.alloc(u8, length);
- }
- _ = try reader.readAll(result.text.?);
-},
- 2 => {
- result.location = try Location.decode(allocator, reader);
-},
- else => {
- return error.InvalidMessage;
- }
- }}
-}
-
-pub fn encode(result: *const @This(), writer: anytype) anyerror!void {
-
- if (result.text) |text| {
- try writer.writeByte(1);
- try writer.writeIntNative(u32, @intCast(u32, text.len));
- try writer.writeAll(std.mem.sliceAsBytes(text));
- }
-
- if (result.location) |location| {
- try writer.writeByte(2);
- try location.encode(writer);
- }
- try writer.writeByte(0);
- return;
-}
-
-};
-
-pub const Message = struct {
-/// kind
-kind: MessageKind,
-
-/// data
-data: MessageData,
-
-/// notes
-notes: []MessageData,
-
-
-pub fn decode(allocator: *std.mem.Allocator, reader: anytype) anyerror!Message {
-var obj = std.mem.zeroes(Message);
-try update(&obj, allocator, reader);
-return obj;
-}
-pub fn update(result: *Message, allocator: *std.mem.Allocator, reader: anytype) anyerror!void {
-
- var length: usize = 0;
- result.kind = try reader.readEnum(MessageKind, .Little);
- result.data = try MessageData.decode(allocator, reader);
- length = try reader.readIntNative(u32);
- result.notes = try allocator.alloc(MessageData, length);
- {
- var j: usize = 0;
- while(j < length) : (j += 1) {
- result.notes[j] = try MessageData.decode(allocator, reader);
- }}
- return;
-}
-
-pub fn encode(result: *const @This(), writer: anytype) anyerror!void {
-
- var n: usize = 0;
- try writer.writeIntNative(@TypeOf(@enumToInt(result.kind)), @enumToInt(result.kind));
-
- try result.data.encode(writer);
-
- n = result.notes.len;
- _ = try writer.writeIntNative(u32, @intCast(u32, n));
- {
- var j: usize = 0;
- while (j < n) : (j += 1) {
- try result.notes[j].encode(writer);
-
- }}
- return;
-}
-
-};
-
-pub const Log = struct {
-/// warnings
-warnings: u32 = 0,
-
-/// errors
-errors: u32 = 0,
-
-/// msgs
-msgs: []Message,
-
-
-pub fn decode(allocator: *std.mem.Allocator, reader: anytype) anyerror!Log {
-var obj = std.mem.zeroes(Log);
-try update(&obj, allocator, reader);
-return obj;
-}
-pub fn update(result: *Log, allocator: *std.mem.Allocator, reader: anytype) anyerror!void {
-
- var length: usize = 0;
- _ = try reader.readAll(std.mem.asBytes(&result.warnings));
- _ = try reader.readAll(std.mem.asBytes(&result.errors));
- length = try reader.readIntNative(u32);
- result.msgs = try allocator.alloc(Message, length);
- {
- var j: usize = 0;
- while(j < length) : (j += 1) {
- result.msgs[j] = try Message.decode(allocator, reader);
- }}
- return;
-}
-
-pub fn encode(result: *const @This(), writer: anytype) anyerror!void {
-
- var n: usize = 0;
- try writer.writeIntNative(u32, result.warnings);
-
- try writer.writeIntNative(u32, result.errors);
-
- n = result.msgs.len;
- _ = try writer.writeIntNative(u32, @intCast(u32, n));
- {
- var j: usize = 0;
- while (j < n) : (j += 1) {
- try result.msgs[j].encode(writer);
-
- }}
- return;
-}
-
+ }
+
+ n = result.loader_values.len;
+ _ = try writer.writeIntNative(u32, @intCast(u32, n));
+ {
+ var j: usize = 0;
+ while (j < n) : (j += 1) {
+ try writer.writeByte(@enumToInt(result.loader_values[j]));
+ }
+ }
+ return;
+ }
+ };
+
+ pub const TransformOptions = struct {
+ /// jsx
+ jsx: Jsx,
+
+ /// ts
+ ts: bool = false,
+
+ /// base_path
+ base_path: []u8,
+
+ /// define_keys
+ define_keys: [][]u8,
+
+ /// define_values
+ define_values: [][]u8,
+
+ pub fn decode(allocator: *std.mem.Allocator, reader: anytype) anyerror!TransformOptions {
+ var obj = std.mem.zeroes(TransformOptions);
+ try update(&obj, allocator, reader);
+ return obj;
+ }
+ pub fn update(result: *TransformOptions, allocator: *std.mem.Allocator, reader: anytype) anyerror!void {
+ var length: usize = 0;
+ result.jsx = try Jsx.decode(allocator, reader);
+ result.ts = (try reader.readByte()) == @as(u8, 1);
+ length = try reader.readIntNative(u32);
+ if (result.base_path.len != length) {
+ result.base_path = try allocator.alloc(u8, length);
+ }
+ _ = try reader.readAll(result.base_path);
+ {
+ var array_count = try reader.readIntNative(u32);
+ if (array_count != result.define_keys.len) {
+ result.define_keys = try allocator.alloc([]u8, array_count);
+ }
+ length = try reader.readIntNative(u32);
+ for (result.define_keys) |content, j| {
+ if (result.define_keys[j].len != length and length > 0) {
+ result.define_keys[j] = try allocator.alloc(u8, length);
+ }
+ _ = try reader.readAll(result.define_keys[j]);
+ }
+ }
+ {
+ var array_count = try reader.readIntNative(u32);
+ if (array_count != result.define_values.len) {
+ result.define_values = try allocator.alloc([]u8, array_count);
+ }
+ length = try reader.readIntNative(u32);
+ for (result.define_values) |content, j| {
+ if (result.define_values[j].len != length and length > 0) {
+ result.define_values[j] = try allocator.alloc(u8, length);
+ }
+ _ = try reader.readAll(result.define_values[j]);
+ }
+ }
+ return;
+ }
+
+ pub fn encode(result: *const @This(), writer: anytype) anyerror!void {
+ var n: usize = 0;
+ try result.jsx.encode(writer);
+
+ try writer.writeByte(@boolToInt(result.ts));
+
+ try writer.writeIntNative(u32, @intCast(u32, result.base_path.len));
+ try writer.writeAll(std.mem.sliceAsBytes(result.base_path));
+
+ n = result.define_keys.len;
+ _ = try writer.writeIntNative(u32, @intCast(u32, n));
+ {
+ var j: usize = 0;
+ while (j < n) : (j += 1) {
+ _ = try writer.writeIntNative(u32, @intCast(u32, result.define_keys[j].len));
+ try writer.writeAll(std.mem.sliceAsBytes(result.define_keys[j]));
+ }
+ }
+
+ n = result.define_values.len;
+ _ = try writer.writeIntNative(u32, @intCast(u32, n));
+ {
+ var j: usize = 0;
+ while (j < n) : (j += 1) {
+ _ = try writer.writeIntNative(u32, @intCast(u32, result.define_values[j].len));
+ try writer.writeAll(std.mem.sliceAsBytes(result.define_values[j]));
+ }
+ }
+ return;
+ }
+ };
+
+ pub const FileHandle = struct {
+ /// path
+ path: []u8,
+
+ /// size
+ size: u32 = 0,
+
+ /// fd
+ fd: u32 = 0,
+
+ pub fn decode(allocator: *std.mem.Allocator, reader: anytype) anyerror!FileHandle {
+ var obj = std.mem.zeroes(FileHandle);
+ try update(&obj, allocator, reader);
+ return obj;
+ }
+ pub fn update(result: *FileHandle, allocator: *std.mem.Allocator, reader: anytype) anyerror!void {
+ var length: usize = 0;
+ length = try reader.readIntNative(u32);
+ if (result.path.len != length) {
+ result.path = try allocator.alloc(u8, length);
+ }
+ _ = try reader.readAll(result.path);
+ _ = try reader.readAll(std.mem.asBytes(&result.size));
+ _ = try reader.readAll(std.mem.asBytes(&result.fd));
+ return;
+ }
+
+ pub fn encode(result: *const @This(), writer: anytype) anyerror!void {
+ try writer.writeIntNative(u32, @intCast(u32, result.path.len));
+ try writer.writeAll(std.mem.sliceAsBytes(result.path));
+
+ try writer.writeIntNative(u32, result.size);
+
+ try writer.writeIntNative(u32, result.fd);
+ return;
+ }
+ };
+
+ pub const Transform = struct {
+ /// handle
+ handle: ?FileHandle = null,
+
+ /// path
+ path: ?[]u8 = null,
+
+ /// contents
+ contents: []u8,
+
+ /// loader
+ loader: ?Loader = null,
+
+ /// options
+ options: ?TransformOptions = null,
+
+ pub fn decode(allocator: *std.mem.Allocator, reader: anytype) anyerror!Transform {
+ var obj = std.mem.zeroes(Transform);
+ try update(&obj, allocator, reader);
+ return obj;
+ }
+ pub fn update(result: *Transform, allocator: *std.mem.Allocator, reader: anytype) anyerror!void {
+ var length: usize = 0;
+ while (true) {
+ const field_type: u8 = try reader.readByte();
+ switch (field_type) {
+ 0 => {
+ return;
+ },
+
+ 1 => {
+ result.handle = try FileHandle.decode(allocator, reader);
+ },
+ 2 => {
+ length = try reader.readIntNative(u32);
+ if ((result.path orelse &([_]u8{})).len != length) {
+ result.path = try allocator.alloc(u8, length);
+ }
+ _ = try reader.readAll(result.path.?);
+ },
+ 3 => {
+ length = @intCast(usize, try reader.readIntNative(u32));
+ if (result.contents.len != length) {
+ result.contents = try allocator.alloc(u8, length);
+ }
+ _ = try reader.readAll(result.contents);
+ },
+ 4 => {
+ result.loader = try reader.readEnum(Loader, .Little);
+ },
+ 5 => {
+ result.options = try TransformOptions.decode(allocator, reader);
+ },
+ else => {
+ return error.InvalidMessage;
+ },
+ }
+ }
+ }
+
+ pub fn encode(result: *const @This(), writer: anytype) anyerror!void {
+ if (result.handle) |handle| {
+ try writer.writeByte(1);
+ try handle.encode(writer);
+ }
+
+ if (result.path) |path| {
+ try writer.writeByte(2);
+ try writer.writeIntNative(u32, @intCast(u32, path.len));
+ try writer.writeAll(std.mem.sliceAsBytes(path));
+ }
+
+ if (result.contents) |contents| {
+ try writer.writeByte(3);
+ try writer.writeIntNative(u32, @intCast(u32, contents.len));
+ try writer.writeAll(contents);
+ }
+
+ if (result.loader) |loader| {
+ try writer.writeByte(4);
+ try writer.writeIntNative(@TypeOf(@enumToInt(result.loader orelse unreachable)), @enumToInt(result.loader orelse unreachable));
+ }
+
+ if (result.options) |options| {
+ try writer.writeByte(5);
+ try options.encode(writer);
+ }
+ try writer.writeByte(0);
+ return;
+ }
+ };
+
+ pub const TransformResponseStatus = enum(u32) {
+ _none,
+ /// success
+ success,
+
+ /// fail
+ fail,
+
+ _,
+
+ pub fn jsonStringify(self: *const @This(), opts: anytype, o: anytype) !void {
+ return try std.json.stringify(@tagName(self), opts, o);
+ }
+ };
+
+ pub const OutputFile = struct {
+ /// data
+ data: []u8,
+
+ /// path
+ path: []u8,
+
+ pub fn decode(allocator: *std.mem.Allocator, reader: anytype) anyerror!OutputFile {
+ var obj = std.mem.zeroes(OutputFile);
+ try update(&obj, allocator, reader);
+ return obj;
+ }
+ pub fn update(result: *OutputFile, allocator: *std.mem.Allocator, reader: anytype) anyerror!void {
+ var length: usize = 0;
+ length = @intCast(usize, try reader.readIntNative(u32));
+ if (result.data != length) {
+ result.data = try allocator.alloc(u8, length);
+ }
+ _ = try reader.readAll(result.data);
+ length = try reader.readIntNative(u32);
+ if (result.path.len != length) {
+ result.path = try allocator.alloc(u8, length);
+ }
+ _ = try reader.readAll(result.path);
+ return;
+ }
+
+ pub fn encode(result: *const @This(), writer: anytype) anyerror!void {
+ try writer.writeIntNative(u32, @intCast(u32, result.data.len));
+ try writer.writeAll(result.data);
+
+ try writer.writeIntNative(u32, @intCast(u32, result.path.len));
+ try writer.writeAll(std.mem.sliceAsBytes(result.path));
+ return;
+ }
+ };
+
+ pub const TransformResponse = struct {
+ /// status
+ status: TransformResponseStatus,
+
+ /// files
+ files: []OutputFile,
+
+ /// errors
+ errors: []Message,
+
+ pub fn decode(allocator: *std.mem.Allocator, reader: anytype) anyerror!TransformResponse {
+ var obj = std.mem.zeroes(TransformResponse);
+ try update(&obj, allocator, reader);
+ return obj;
+ }
+ pub fn update(result: *TransformResponse, allocator: *std.mem.Allocator, reader: anytype) anyerror!void {
+ var length: usize = 0;
+ result.status = try reader.readEnum(TransformResponseStatus, .Little);
+ length = try reader.readIntNative(u32);
+ result.files = try allocator.alloc(OutputFile, length);
+ {
+ var j: usize = 0;
+ while (j < length) : (j += 1) {
+ result.files[j] = try OutputFile.decode(allocator, reader);
+ }
+ }
+ length = try reader.readIntNative(u32);
+ result.errors = try allocator.alloc(Message, length);
+ {
+ var j: usize = 0;
+ while (j < length) : (j += 1) {
+ result.errors[j] = try Message.decode(allocator, reader);
+ }
+ }
+ return;
+ }
+
+ pub fn encode(result: *const @This(), writer: anytype) anyerror!void {
+ var n: usize = 0;
+ try writer.writeIntNative(@TypeOf(@enumToInt(result.status)), @enumToInt(result.status));
+
+ n = result.files.len;
+ _ = try writer.writeIntNative(u32, @intCast(u32, n));
+ {
+ var j: usize = 0;
+ while (j < n) : (j += 1) {
+ try result.files[j].encode(writer);
+ }
+ }
+
+ n = result.errors.len;
+ _ = try writer.writeIntNative(u32, @intCast(u32, n));
+ {
+ var j: usize = 0;
+ while (j < n) : (j += 1) {
+ try result.errors[j].encode(writer);
+ }
+ }
+ return;
+ }
+ };
+
+ pub const MessageKind = enum(u32) {
+ _none,
+ /// err
+ err,
+
+ /// warn
+ warn,
+
+ /// note
+ note,
+
+ /// debug
+ debug,
+
+ _,
+
+ pub fn jsonStringify(self: *const @This(), opts: anytype, o: anytype) !void {
+ return try std.json.stringify(@tagName(self), opts, o);
+ }
+ };
+
+ pub const Location = struct {
+ /// file
+ file: []u8,
+
+ /// namespace
+ namespace: []u8,
+
+ /// line
+ line: i32 = 0,
+
+ /// column
+ column: i32 = 0,
+
+ /// line_text
+ line_text: []u8,
+
+ /// suggestion
+ suggestion: []u8,
+
+ /// offset
+ offset: u32 = 0,
+
+ pub fn decode(allocator: *std.mem.Allocator, reader: anytype) anyerror!Location {
+ var obj = std.mem.zeroes(Location);
+ try update(&obj, allocator, reader);
+ return obj;
+ }
+ pub fn update(result: *Location, allocator: *std.mem.Allocator, reader: anytype) anyerror!void {
+ var length: usize = 0;
+ length = try reader.readIntNative(u32);
+ if (result.file.len != length) {
+ result.file = try allocator.alloc(u8, length);
+ }
+ _ = try reader.readAll(result.file);
+ length = try reader.readIntNative(u32);
+ if (result.namespace.len != length) {
+ result.namespace = try allocator.alloc(u8, length);
+ }
+ _ = try reader.readAll(result.namespace);
+ _ = try reader.readAll(std.mem.asBytes(&result.line));
+ _ = try reader.readAll(std.mem.asBytes(&result.column));
+ length = try reader.readIntNative(u32);
+ if (result.line_text.len != length) {
+ result.line_text = try allocator.alloc(u8, length);
+ }
+ _ = try reader.readAll(result.line_text);
+ length = try reader.readIntNative(u32);
+ if (result.suggestion.len != length) {
+ result.suggestion = try allocator.alloc(u8, length);
+ }
+ _ = try reader.readAll(result.suggestion);
+ _ = try reader.readAll(std.mem.asBytes(&result.offset));
+ return;
+ }
+
+ pub fn encode(result: *const @This(), writer: anytype) anyerror!void {
+ try writer.writeIntNative(u32, @intCast(u32, result.file.len));
+ try writer.writeAll(std.mem.sliceAsBytes(result.file));
+
+ try writer.writeIntNative(u32, @intCast(u32, result.namespace.len));
+ try writer.writeAll(std.mem.sliceAsBytes(result.namespace));
+
+ try writer.writeIntNative(i32, result.line);
+
+ try writer.writeIntNative(i32, result.column);
+
+ try writer.writeIntNative(u32, @intCast(u32, result.line_text.len));
+ try writer.writeAll(std.mem.sliceAsBytes(result.line_text));
+
+ try writer.writeIntNative(u32, @intCast(u32, result.suggestion.len));
+ try writer.writeAll(std.mem.sliceAsBytes(result.suggestion));
+
+ try writer.writeIntNative(u32, result.offset);
+ return;
+ }
+ };
+
+ pub const MessageData = struct {
+ /// text
+ text: ?[]u8 = null,
+
+ /// location
+ location: ?Location = null,
+
+ pub fn decode(allocator: *std.mem.Allocator, reader: anytype) anyerror!MessageData {
+ var obj = std.mem.zeroes(MessageData);
+ try update(&obj, allocator, reader);
+ return obj;
+ }
+ pub fn update(result: *MessageData, allocator: *std.mem.Allocator, reader: anytype) anyerror!void {
+ var length: usize = 0;
+ while (true) {
+ const field_type: u8 = try reader.readByte();
+ switch (field_type) {
+ 0 => {
+ return;
+ },
+
+ 1 => {
+ length = try reader.readIntNative(u32);
+ if ((result.text orelse &([_]u8{})).len != length) {
+ result.text = try allocator.alloc(u8, length);
+ }
+ _ = try reader.readAll(result.text.?);
+ },
+ 2 => {
+ result.location = try Location.decode(allocator, reader);
+ },
+ else => {
+ return error.InvalidMessage;
+ },
+ }
+ }
+ }
+
+ pub fn encode(result: *const @This(), writer: anytype) anyerror!void {
+ if (result.text) |text| {
+ try writer.writeByte(1);
+ try writer.writeIntNative(u32, @intCast(u32, text.len));
+ try writer.writeAll(std.mem.sliceAsBytes(text));
+ }
+
+ if (result.location) |location| {
+ try writer.writeByte(2);
+ try location.encode(writer);
+ }
+ try writer.writeByte(0);
+ return;
+ }
+ };
+
+ pub const Message = struct {
+ /// kind
+ kind: MessageKind,
+
+ /// data
+ data: MessageData,
+
+ /// notes
+ notes: []MessageData,
+
+ pub fn decode(allocator: *std.mem.Allocator, reader: anytype) anyerror!Message {
+ var obj = std.mem.zeroes(Message);
+ try update(&obj, allocator, reader);
+ return obj;
+ }
+ pub fn update(result: *Message, allocator: *std.mem.Allocator, reader: anytype) anyerror!void {
+ var length: usize = 0;
+ result.kind = try reader.readEnum(MessageKind, .Little);
+ result.data = try MessageData.decode(allocator, reader);
+ length = try reader.readIntNative(u32);
+ result.notes = try allocator.alloc(MessageData, length);
+ {
+ var j: usize = 0;
+ while (j < length) : (j += 1) {
+ result.notes[j] = try MessageData.decode(allocator, reader);
+ }
+ }
+ return;
+ }
+
+ pub fn encode(result: *const @This(), writer: anytype) anyerror!void {
+ var n: usize = 0;
+ try writer.writeIntNative(@TypeOf(@enumToInt(result.kind)), @enumToInt(result.kind));
+
+ try result.data.encode(writer);
+
+ n = result.notes.len;
+ _ = try writer.writeIntNative(u32, @intCast(u32, n));
+ {
+ var j: usize = 0;
+ while (j < n) : (j += 1) {
+ try result.notes[j].encode(writer);
+ }
+ }
+ return;
+ }
+ };
+
+ pub const Log = struct {
+ /// warnings
+ warnings: u32 = 0,
+
+ /// errors
+ errors: u32 = 0,
+
+ /// msgs
+ msgs: []Message,
+
+ pub fn decode(allocator: *std.mem.Allocator, reader: anytype) anyerror!Log {
+ var obj = std.mem.zeroes(Log);
+ try update(&obj, allocator, reader);
+ return obj;
+ }
+ pub fn update(result: *Log, allocator: *std.mem.Allocator, reader: anytype) anyerror!void {
+ var length: usize = 0;
+ _ = try reader.readAll(std.mem.asBytes(&result.warnings));
+ _ = try reader.readAll(std.mem.asBytes(&result.errors));
+ length = try reader.readIntNative(u32);
+ result.msgs = try allocator.alloc(Message, length);
+ {
+ var j: usize = 0;
+ while (j < length) : (j += 1) {
+ result.msgs[j] = try Message.decode(allocator, reader);
+ }
+ }
+ return;
+ }
+
+ pub fn encode(result: *const @This(), writer: anytype) anyerror!void {
+ var n: usize = 0;
+ try writer.writeIntNative(u32, result.warnings);
+
+ try writer.writeIntNative(u32, result.errors);
+
+ n = result.msgs.len;
+ _ = try writer.writeIntNative(u32, @intCast(u32, n));
+ {
+ var j: usize = 0;
+ while (j < n) : (j += 1) {
+ try result.msgs[j].encode(writer);
+ }
+ }
+ return;
+ }
+ };
};
-
-
-}; \ No newline at end of file
diff --git a/src/js_printer.zig b/src/js_printer.zig
index 1ed418e47..8322d8e3a 100644
--- a/src/js_printer.zig
+++ b/src/js_printer.zig
@@ -2005,6 +2005,7 @@ pub fn NewPrinter(comptime ascii_only: bool) type {
p.printIndent();
p.printSpaceBeforeIdentifier();
p.print("export default");
+
p.printSpace();
switch (s.value) {
@@ -2026,7 +2027,10 @@ pub fn NewPrinter(comptime ascii_only: bool) type {
if (func.func.flags.is_generator) {
p.print("*");
p.printSpace();
+ } else {
+ p.maybePrintSpace();
}
+
if (func.func.name) |name| {
p.printSymbol(name.ref orelse Global.panic("Internal error: Expected func to have a name ref\n{s}", .{func}));
}
@@ -2729,6 +2733,7 @@ pub fn NewPrinter(comptime ascii_only: bool) type {
}
pub fn init(allocator: *std.mem.Allocator, tree: Ast, source: *logger.Source, symbols: Symbol.Map, opts: Options, linker: *Linker) !Printer {
+ // Heuristic: most lines of JavaScript are short.
var js = try MutableString.init(allocator, 0);
return Printer{
.allocator = allocator,
diff --git a/src/main_wasm.zig b/src/main_wasm.zig
index 49f7d36c5..026e310eb 100644
--- a/src/main_wasm.zig
+++ b/src/main_wasm.zig
@@ -59,7 +59,8 @@ pub const Uint8Array = packed struct {
pub fn decode(self: Abi, comptime SchemaType: type) !SchemaType {
var buf = Uint8Array.toSlice(self);
var stream = std.io.fixedBufferStream(buf);
- return try SchemaType.decode(alloc.dynamic, stream.reader());
+ const res = try SchemaType.decode(alloc.dynamic, stream.reader());
+ return res;
}
};
@@ -74,13 +75,13 @@ pub const Api = struct {
defines: ?*Define = null,
pub fn transform(self: *Api, request: Schema.Transform) !Schema.TransformResponse {
- const opts = try options.TransformOptions.initUncached(alloc.dynamic, request.path.?, request.contents.?);
+ const opts = try options.TransformOptions.initUncached(alloc.dynamic, request.path.?, request.contents);
var source = logger.Source.initFile(opts.entry_point, alloc.dynamic);
var ast: js_ast.Ast = undefined;
if (self.defines == null) {
var raw_defines = RawDefines.init(alloc.static);
- try raw_defines.put("process.env.NODE_ENV", "\"development\"");
+ raw_defines.put("process.env.NODE_ENV", "\"development\"") catch unreachable;
var user_defines = try DefineData.from_input(raw_defines, &self.log, alloc.static);
self.defines = try Define.init(
@@ -124,6 +125,7 @@ pub const Api = struct {
js_printer.Options{ .to_module_ref = ast.module_ref orelse js_ast.Ref{ .inner_index = 0 } },
&_linker,
);
+ // Output.print("Parts count: {d}", .{ast.parts.len});
var output_files = try alloc.dynamic.alloc(Schema.OutputFile, 1);
var _data = printed.js[0..printed.js.len];
var _path = constStrToU8(source.path.text);
@@ -144,52 +146,74 @@ pub extern fn console_error(abi: Uint8Array.Abi) void;
pub extern fn console_warn(abi: Uint8Array.Abi) void;
pub extern fn console_info(abi: Uint8Array.Abi) void;
+const ZeeAlloc = zee.ZeeAlloc(.{});
+var arena: std.heap.ArenaAllocator = undefined;
pub const Exports = struct {
fn init() callconv(.C) i32 {
- // const res = @wasmMemoryGrow(0, amount_to_grow);
+ // const Gpa = std.heap.GeneralPurposeAllocator(.{});
+ // var gpa = Gpa{};
+ // var allocator = &gpa.allocator;
+ // alloc.setup(allocator) catch return -1;
+ var out_buffer = std.heap.page_allocator.alloc(u8, 2048) catch return -1;
+ var err_buffer = std.heap.page_allocator.alloc(u8, 2048) catch return -1;
+ var output = std.heap.page_allocator.create(Output.Source) catch return -1;
+ var stream = std.io.fixedBufferStream(out_buffer);
+ var err_stream = std.io.fixedBufferStream(err_buffer);
+ output.* = Output.Source.init(
+ stream,
+ err_stream,
+ );
+ output.out_buffer = out_buffer;
+ output.err_buffer = err_buffer;
+ Output.Source.set(output);
+
+ var _api = std.heap.page_allocator.create(Api) catch return -1;
+ _api.* = Api{ .files = std.ArrayList(string).init(std.heap.page_allocator), .log = logger.Log.init(std.heap.page_allocator) };
+ api = _api;
+
+ _ = MainPanicHandler.init(&api.?.log);
+
+ // This will need more thought.
+ var raw_defines = RawDefines.init(std.heap.page_allocator);
+ raw_defines.put("process.env.NODE_ENV", "\"development\"") catch return -1;
+ var user_defines = DefineData.from_input(raw_defines, &_api.log, std.heap.page_allocator) catch return -1;
+ _api.defines = Define.init(
+ std.heap.page_allocator,
+ user_defines,
+ ) catch return -1;
+
if (alloc.needs_setup) {
- alloc.setup(zee.ZeeAllocDefaults.wasm_allocator) catch return -1;
- // const Gpa = std.heap.GeneralPurposeAllocator(.{});
- // var gpa = Gpa{};
- // var allocator = &gpa.allocator;
- // alloc.setup(allocator) catch return -1;
- var out_buffer = alloc.static.alloc(u8, 2048) catch return -1;
- var err_buffer = alloc.static.alloc(u8, 2048) catch return -1;
- var output = alloc.static.create(Output.Source) catch return -1;
- var stream = std.io.fixedBufferStream(out_buffer);
- var err_stream = std.io.fixedBufferStream(err_buffer);
- output.* = Output.Source.init(
- stream,
- err_stream,
- );
- output.out_buffer = out_buffer;
- output.err_buffer = err_buffer;
- Output.Source.set(output);
+ arena = std.heap.ArenaAllocator.init(ZeeAlloc.wasm_allocator);
+ alloc.setup(&arena.allocator) catch return -1;
}
- var _api = alloc.static.create(Api) catch return -1;
- _api.* = Api{ .files = std.ArrayList(string).init(alloc.dynamic), .log = logger.Log.init(alloc.dynamic) };
- api = _api;
+ _ = @wasmMemoryGrow(0, 300);
+
Output.printErrorable("Initialized.", .{}) catch |err| {
var name = alloc.static.alloc(u8, @errorName(err).len) catch unreachable;
std.mem.copy(u8, name, @errorName(err));
console_error(Uint8Array.fromSlice(name));
};
- _ = MainPanicHandler.init(&api.?.log);
-
return 1;
}
fn transform(abi: Uint8Array.Abi) callconv(.C) Uint8Array.Abi {
- Output.print("Received {d}", .{abi});
+ // Output.print("Received {d}", .{abi});
const req: Schema.Transform = Uint8Array.decode(abi, Schema.Transform) catch return Uint8Array.empty();
- Output.print("Req {s}", .{req});
+ // Output.print("Req {s}", .{req});
// alloc.dynamic.free(Uint8Array.toSlice(abi));
const resp = api.?.transform(req) catch return Uint8Array.empty();
return Uint8Array.encode(Schema.TransformResponse, resp) catch return Uint8Array.empty();
}
+ // Reset
+ fn cycle() callconv(.C) void {
+ arena.deinit();
+ arena = std.heap.ArenaAllocator.init(ZeeAlloc.wasm_allocator);
+ alloc.setup(&arena.allocator) catch return;
+ }
+
fn malloc(size: usize) callconv(.C) ?*c_void {
if (size == 0) {
return null;
@@ -238,6 +262,7 @@ comptime {
@export(Exports.malloc, .{ .name = "malloc", .linkage = .Strong });
@export(Exports.calloc, .{ .name = "calloc", .linkage = .Strong });
@export(Exports.realloc, .{ .name = "realloc", .linkage = .Strong });
+ @export(Exports.cycle, .{ .name = "cycle", .linkage = .Strong });
@export(Exports.free, .{ .name = "free", .linkage = .Strong });
}
diff --git a/src/string_mutable.zig b/src/string_mutable.zig
index f142f5101..a5b13ca2e 100644
--- a/src/string_mutable.zig
+++ b/src/string_mutable.zig
@@ -15,16 +15,11 @@ pub const MutableString = struct {
}
pub fn growIfNeeded(self: *MutableString, amount: usize) !void {
- const new_capacity = self.list.items.len + amount;
- if (self.list.capacity < new_capacity) {
- try self.list.ensureCapacity(self.allocator, new_capacity);
- }
+ try self.list.ensureUnusedCapacity(self.allocator, amount);
}
pub fn writeAll(self: *MutableString, bytes: string) !usize {
- const new_capacity = self.list.items.len + bytes.len;
- try self.list.ensureCapacity(self.allocator, new_capacity);
- self.list.appendSliceAssumeCapacity(bytes);
+ try self.list.appendSlice(self.allocator, bytes);
return self.list.items.len;
}
@@ -47,26 +42,46 @@ pub const MutableString = struct {
return "_";
}
- var mutable = try MutableString.init(allocator, 0);
-
- var needsGap = false;
- for (str) |c| {
- if (std.ascii.isLower(c) or std.ascii.isUpper(c) or (mutable.len() > 0 and std.ascii.isAlNum(c))) {
- if (needsGap) {
- try mutable.appendChar('_');
- needsGap = false;
+ var has_needed_gap = false;
+ var needs_gap = false;
+ var start_i: usize = 0;
+
+ // Common case: no gap necessary. No allocation necessary.
+ needs_gap = std.ascii.isAlNum(str[0]);
+ if (!needs_gap) {
+ // Are there any non-alphanumeric chars at all?
+ for (str[1..str.len]) |c, i| {
+ switch (c) {
+ 'a'...'z', 'A'...'Z', '0'...'9' => {},
+ else => {
+ needs_gap = true;
+ start_i = i;
+ break;
+ },
}
- try mutable.appendChar(c);
- } else if (!needsGap) {
- needsGap = true;
}
}
- if (mutable.len() > 0) {
+ if (needs_gap) {
+ var mutable = try MutableString.initCopy(allocator, str[0..start_i]);
+
+ for (str[start_i..str.len]) |c, i| {
+ if (std.ascii.isLower(c) or std.ascii.isUpper(c) or (mutable.len() > 0 and std.ascii.isAlNum(c))) {
+ if (needs_gap) {
+ try mutable.appendChar('_');
+ needs_gap = false;
+ has_needed_gap = true;
+ }
+ try mutable.appendChar(c);
+ } else if (!needs_gap) {
+ needs_gap = true;
+ }
+ }
+
return mutable.list.toOwnedSlice(allocator);
- } else {
- return str;
}
+
+ return str;
}
pub fn len(self: *MutableString) usize {
diff --git a/src/zee_alloc.zig b/src/zee_alloc.zig
index ab98fe854..b1f56e726 100644
--- a/src/zee_alloc.zig
+++ b/src/zee_alloc.zig
@@ -28,7 +28,7 @@ pub fn ZeeAlloc(comptime conf: Config) type {
/// The definitiveā„¢ way of using `ZeeAlloc`
pub const wasm_allocator = &_wasm.allocator;
- var _wasm = init(&wasm_page_allocator);
+ pub var _wasm = init(&wasm_page_allocator);
jumbo: ?*Slab = null,
slabs: [total_slabs]?*Slab = [_]?*Slab{null} ** total_slabs,
@@ -157,7 +157,7 @@ pub fn ZeeAlloc(comptime conf: Config) type {
return .{ .backing_allocator = allocator };
}
- pub fn deinit(self: *Self) void {
+ pub fn freeAll(self: *Self) void {
{
var iter = self.jumbo;
while (iter) |node| {
@@ -174,6 +174,10 @@ pub fn ZeeAlloc(comptime conf: Config) type {
self.backing_allocator.destroy(node);
}
}
+ }
+
+ pub fn deinit(self: *Self) void {
+ self.freeAll();
self.* = undefined;
}
@@ -285,7 +289,7 @@ pub fn ZeeAlloc(comptime conf: Config) type {
};
}
-var wasm_page_allocator = init: {
+pub var wasm_page_allocator = init: {
if (!std.builtin.target.isWasm()) {
@compileError("wasm allocator is only available for wasm32 arch");
}