mirror of
https://github.com/oven-sh/bun
synced 2026-02-16 05:42:43 +00:00
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
131
src/api/demo/pnpm-lock.yaml
generated
131
src/api/demo/pnpm-lock.yaml
generated
@@ -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'}
|
||||
|
||||
@@ -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"];
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -101,7 +101,7 @@ type uint32 = number;
|
||||
export interface Transform {
|
||||
handle?: FileHandle;
|
||||
path?: string;
|
||||
contents?: string;
|
||||
contents?: Uint8Array;
|
||||
loader?: Loader;
|
||||
options?: TransformOptions;
|
||||
}
|
||||
|
||||
1474
src/api/schema.zig
1474
src/api/schema.zig
File diff suppressed because it is too large
Load Diff
@@ -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,
|
||||
|
||||
@@ -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 });
|
||||
}
|
||||
|
||||
|
||||
@@ -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 has_needed_gap = false;
|
||||
var needs_gap = false;
|
||||
var start_i: usize = 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;
|
||||
// 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 {
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user