Skip to content

Commit

Permalink
fix: Removed Parsed.DEFAULT_WASM_BINARY_URL
Browse files Browse the repository at this point in the history
  • Loading branch information
robertoraggi committed Sep 17, 2023
1 parent b96612d commit 5ff941c
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 15 deletions.
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ npm pack
// example.mjs
//

import DEFAULT_WASM_BINARY_URL from "../dist/defaultWasmBinaryUrl.js";
import { Parser, AST, ASTKind } from "cxx-frontend";
import { readFile } from "fs/promises";
import { fileURLToPath } from "url";
Expand All @@ -104,7 +105,7 @@ int main() {
`;

async function main() {
const wasmBinaryFile = fileURLToPath(Parser.DEFAULT_WASM_BINARY_URL);
const wasmBinaryFile = fileURLToPath(DEFAULT_WASM_BINARY_URL);

const wasmBinary = await readFile(wasmBinaryFile);

Expand Down Expand Up @@ -147,13 +148,11 @@ main().catch(console.error);
</head>
<body>
<script type="module">
import {
Parser,
AST,
ASTKind,
} from "https://unpkg.com/cxx-frontend@latest/dist/index.js";
import { Parser, AST, ASTKind } from "https://unpkg.com/cxx-frontend";

const response = await fetch(Parser.DEFAULT_WASM_BINARY_URL);
const response = await fetch(
"https://unpkg.com/cxx-frontend/dist/cxx-js.wasm"
);

const wasmBinary = new Uint8Array(await response.arrayBuffer());

Expand Down
3 changes: 2 additions & 1 deletion packages/cxx-frontend/examples/dump.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// dump.mjs
//

import DEFAULT_WASM_BINARY_URL from "../dist/defaultWasmBinaryUrl.js";
import { Parser, AST, ASTKind } from "../dist/index.js";
import { readFile } from "fs/promises";
import { fileURLToPath } from "url";
Expand All @@ -25,7 +26,7 @@ int main() {
`;

async function main() {
const wasmBinaryFile = fileURLToPath(Parser.DEFAULT_WASM_BINARY_URL);
const wasmBinaryFile = fileURLToPath(DEFAULT_WASM_BINARY_URL);

const wasmBinary = await readFile(wasmBinaryFile);

Expand Down
3 changes: 2 additions & 1 deletion packages/cxx-frontend/examples/preprocess.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import DEFAULT_WASM_BINARY_URL from "../dist/defaultWasmBinaryUrl.js";
import { Parser, Preprocessor } from "../dist/index.js";
import { readFile } from "fs/promises";
import { fileURLToPath } from "url";
Expand Down Expand Up @@ -30,7 +31,7 @@ constexpr bool unix_target = false;
`;

async function main() {
const wasmBinaryFile = fileURLToPath(Parser.DEFAULT_WASM_BINARY_URL);
const wasmBinaryFile = fileURLToPath(DEFAULT_WASM_BINARY_URL);

const wasmBinary = await readFile(wasmBinaryFile);

Expand Down
3 changes: 2 additions & 1 deletion packages/cxx-frontend/examples/tokenize.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// tokenize.mjs
//

import DEFAULT_WASM_BINARY_URL from "../dist/defaultWasmBinaryUrl.js";
import { Parser, Lexer, TokenKind } from "../dist/index.js";
import { readFile } from "fs/promises";
import { fileURLToPath } from "url";
Expand All @@ -22,7 +23,7 @@ int main() {
`;

async function main() {
const wasmBinaryFile = fileURLToPath(Parser.DEFAULT_WASM_BINARY_URL);
const wasmBinaryFile = fileURLToPath(DEFAULT_WASM_BINARY_URL);

const wasmBinary = await readFile(wasmBinaryFile);

Expand Down
3 changes: 2 additions & 1 deletion packages/cxx-frontend/examples/unit.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import DEFAULT_WASM_BINARY_URL from "../dist/defaultWasmBinaryUrl.js";
import {
Parser,
TokenKind,
Expand All @@ -15,7 +16,7 @@ int main() {
`;

async function main() {
const wasmBinaryFile = fileURLToPath(Parser.DEFAULT_WASM_BINARY_URL);
const wasmBinaryFile = fileURLToPath(DEFAULT_WASM_BINARY_URL);

const wasmBinary = await readFile(wasmBinaryFile);

Expand Down
19 changes: 15 additions & 4 deletions packages/cxx-frontend/src/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,26 @@ export class Parser {
#unit: Unit | undefined;
#ast: AST | undefined;

static DEFAULT_WASM_BINARY_URL = new URL("./cxx-js.wasm", import.meta.url);

static async init({ wasmBinary }: { wasmBinary: Uint8Array }) {
return await initCxx({ wasmBinary });
}

static async initFromURL(url: URL) {
const response = await fetch(url);
static async initFromURL(
url: URL,
{ signal }: { signal?: AbortSignal } = {}
) {
const response = await fetch(url, { signal });

if (!response.ok) {
throw new Error(`failed to fetch '${url}'`);
}

if (signal?.aborted) {
throw new Error(`fetch '${url}' aborted`);
}

const wasmBinary = await response.arrayBuffer();

return await Parser.init({ wasmBinary: new Uint8Array(wasmBinary) });
}

Expand Down
23 changes: 23 additions & 0 deletions packages/cxx-frontend/src/defaultWasmBinaryUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) 2023 Roberto Raggi <roberto.raggi@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

const defaultWasmBinaryUrl = new URL("./cxx-js.wasm", import.meta.url);

export default defaultWasmBinaryUrl;

0 comments on commit 5ff941c

Please sign in to comment.