From 45b04b34131abcf2045008204a4fb03bbe62f07a Mon Sep 17 00:00:00 2001 From: Doehyun Baek Date: Wed, 7 Aug 2024 21:42:10 +0800 Subject: [PATCH] let's try --experimental-strip-types --- .github/workflows/wasm-r3.yml | 2 +- package-lock.json | 22 ++--- package.json | 13 ++- src/{filter.cts => filter.ts} | 0 src/{main.cts => start.ts} | 5 +- .../test-slice-dice.ts | 2 +- tests/test-wasm-r3.cts => src/test.ts | 33 ++++++-- src/{web.cts => web.ts} | 26 +----- tests/test-utils.cts | 82 ------------------- tsconfig.json | 2 + 10 files changed, 55 insertions(+), 132 deletions(-) rename src/{filter.cts => filter.ts} (100%) rename src/{main.cts => start.ts} (94%) rename tests/test-slice-dice.cts => src/test-slice-dice.ts (98%) rename tests/test-wasm-r3.cts => src/test.ts (86%) rename src/{web.cts => web.ts} (91%) delete mode 100644 tests/test-utils.cts diff --git a/.github/workflows/wasm-r3.yml b/.github/workflows/wasm-r3.yml index 64004f36..b349ef6d 100644 --- a/.github/workflows/wasm-r3.yml +++ b/.github/workflows/wasm-r3.yml @@ -19,7 +19,7 @@ jobs: - name: Use Node.js uses: actions/setup-node@v1 with: - node-version: '21.x' + node-version: '22.6.x' - run: npm install && npx playwright install - name: Download Binaryen run: | diff --git a/package-lock.json b/package-lock.json index 24b7c7eb..4f0ac70d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,7 +8,7 @@ "dependencies": { "acorn": "^8.11.2", "command-line-args": "^5.2.1", - "playwright": "^1.42.1", + "playwright": "^1.46.0", "ws": "^8.17.1" }, "devDependencies": { @@ -827,31 +827,33 @@ "dev": true }, "node_modules/playwright": { - "version": "1.42.1", - "resolved": "https://registry.npmjs.org/playwright/-/playwright-1.42.1.tgz", - "integrity": "sha512-PgwB03s2DZBcNRoW+1w9E+VkLBxweib6KTXM0M3tkiT4jVxKSi6PmVJ591J+0u10LUrgxB7dLRbiJqO5s2QPMg==", + "version": "1.46.0", + "resolved": "https://registry.npmjs.org/playwright/-/playwright-1.46.0.tgz", + "integrity": "sha512-XYJ5WvfefWONh1uPAUAi0H2xXV5S3vrtcnXe6uAOgdGi3aSpqOSXX08IAjXW34xitfuOJsvXU5anXZxPSEQiJw==", + "license": "Apache-2.0", "dependencies": { - "playwright-core": "1.42.1" + "playwright-core": "1.46.0" }, "bin": { "playwright": "cli.js" }, "engines": { - "node": ">=16" + "node": ">=18" }, "optionalDependencies": { "fsevents": "2.3.2" } }, "node_modules/playwright-core": { - "version": "1.42.1", - "resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.42.1.tgz", - "integrity": "sha512-mxz6zclokgrke9p1vtdy/COWBH+eOZgYUVVU34C73M+4j4HLlQJHtfcqiqqxpP0o8HhMkflvfbquLX5dg6wlfA==", + "version": "1.46.0", + "resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.46.0.tgz", + "integrity": "sha512-9Y/d5UIwuJk8t3+lhmMSAJyNP1BUC/DqP3cQJDQQL/oWqAiuPTLgy7Q5dzglmTLwcBRdetzgNM/gni7ckfTr6A==", + "license": "Apache-2.0", "bin": { "playwright-core": "cli.js" }, "engines": { - "node": ">=16" + "node": ">=18" } }, "node_modules/proxy-addr": { diff --git a/package.json b/package.json index de2a9189..966381a2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,4 @@ { - "name": "tool", - "module": "index.ts", "type": "module", "devDependencies": { "@types/diff": "^5.0.8", @@ -19,15 +17,14 @@ "dependencies": { "acorn": "^8.11.2", "command-line-args": "^5.2.1", - "playwright": "^1.42.1", + "playwright": "^1.46.0", "ws": "^8.17.1" }, "scripts": { - "start": "npm run build && node ./dist/src/cli/main.cjs", - "test": "npm run build && node --experimental-wasm-multi-memory --experimental-wasm-typed_funcref --experimental-wasm-gc ./dist/tests/test-wasm-r3.cjs", - "build": "npm run build-rust && npm run build-wasabi && tsc", + "start": "node --experimental-strip-types ./src/start.ts", + "test": "node --experimental-strip-types ./src/test.ts", + "build": "npm run build-rust && npm run build-wasabi", "build-rust": "cd crates && cargo build --release && cd -", - "build-wasabi": "cd wasabi/crates/wasabi_js && npm run build && wasm-pack build --target web && wasm-pack build --target nodejs --out-dir ../../../dist/wasabi && cd ../../..", - "build-binaryen": "cd binaryen && git submodule update --init && cmake . && make" + "build-wasabi": "cd wasabi/crates/wasabi_js && npm run build && wasm-pack build --target web && cd ../../.." } } diff --git a/src/filter.cts b/src/filter.ts similarity index 100% rename from src/filter.cts rename to src/filter.ts diff --git a/src/main.cts b/src/start.ts similarity index 94% rename from src/main.cts rename to src/start.ts index 90e6e4ba..38bd9191 100644 --- a/src/main.cts +++ b/src/start.ts @@ -6,8 +6,7 @@ async function main() { } export default async function run(url: string, options: Options) { - let analyser: AnalyserI - analyser = new Analyser('./dist/src/tracer.cjs', options) + let analyser = new Analyser('./dist/src/tracer.cjs', options) await analyser.start(url, { headless: options.headless }) await askQuestion(`Record is running. Enter 'Stop' to stop recording: `) console.log(`Record stopped. Downloading...`) @@ -18,7 +17,7 @@ export default async function run(url: string, options: Options) { import commandLineArgs from 'command-line-args' import fs from 'fs' -import Benchmark, { AnalyserI, Analyser } from './web.cjs' +import Benchmark, { Analyser } from './web.ts' export type Options = { headless: boolean, diff --git a/tests/test-slice-dice.cts b/src/test-slice-dice.ts similarity index 98% rename from tests/test-slice-dice.cts rename to src/test-slice-dice.ts index a8f5da53..88bc9ea6 100644 --- a/tests/test-slice-dice.cts +++ b/src/test-slice-dice.ts @@ -5,7 +5,7 @@ import { exit } from "process"; import { Server } from "http"; import path from "path"; import { execSync } from "child_process"; -import Benchmark, { Analyser } from "../src/web.cjs"; +import Benchmark, { Analyser } from "./web.ts"; export default async function runSliceDiceTests(names: string[], options) { for (let name of names) { diff --git a/tests/test-wasm-r3.cts b/src/test.ts similarity index 86% rename from tests/test-wasm-r3.cts rename to src/test.ts index 1da86830..d3a77b12 100644 --- a/tests/test-wasm-r3.cts +++ b/src/test.ts @@ -1,14 +1,12 @@ import fs from "fs/promises"; import path from "path"; import express from "express"; -//@ts-ignore import { Server } from "http"; import commandLineArgs from "command-line-args"; -import { getDirectoryNames, trimFromLastOccurance, writeWithSpaces } from "./test-utils.cjs"; -import { filter } from "../src/filter.cjs"; -import runSliceDiceTests from "./test-slice-dice.cjs"; -import Benchmark, { Analyser } from "../src/web.cjs"; import { execSync } from "child_process"; +import { filter } from "./filter.ts"; +import Benchmark, { Analyser } from "./web.ts"; +import runSliceDiceTests from "./test-slice-dice.ts"; type Success = { success: true }; type Failure = { success: false }; @@ -149,4 +147,29 @@ async function startServer(websitePath: string): Promise<[Server, string]> { const url = `http://localhost:${address.port}`; return [server, url] +} + +export async function getDirectoryNames(folderPath: string) { + const entries = await fs.readdir(folderPath, { withFileTypes: true }); + + const directories: string[] = entries + .filter((entry) => entry.isDirectory()) + .map((entry) => entry.name); + + return directories; +} + +export async function delay(ms: number) { + return new Promise(resolve => { + setTimeout(resolve, ms); + }); +} + +export function trimFromLastOccurance(str: string, substring: string) { + const lastIndex = str.lastIndexOf(substring); + if (lastIndex === -1) { + // Substring not found, return original string or handle as needed + return str; + } + return str.substring(0, lastIndex + substring.length); } \ No newline at end of file diff --git a/src/web.cts b/src/web.ts similarity index 91% rename from src/web.cts rename to src/web.ts index d357485b..26bf8648 100644 --- a/src/web.cts +++ b/src/web.ts @@ -1,23 +1,15 @@ -import { Browser, chromium, firefox, Frame, Page, webkit, Worker } from 'playwright' import fs from 'fs/promises' -import acorn from 'acorn' -import { trimFromLastOccurance } from '../tests/test-utils.cjs' // read port from env const CDP_PORT = process.env.CDP_PORT || 8080 -export interface AnalyserI { - start: (url: string, options: { headless: boolean }) => Promise - stop: () => Promise -} - export type AnalysisResult = { result: string, wasm: number[] }[] type Options = { extended?: boolean, noRecord?: boolean, evalRecord?: boolean, firefox?: boolean, webkit?: boolean } -export class Analyser implements AnalyserI { +export class Analyser { private analysisPath: string private options: Options @@ -125,7 +117,6 @@ export class Analyser implements AnalyserI { } const script = await response.text() try { - acorn.parse(script, { ecmaVersion: 'latest' }) const body = `${initScript}${script}` await route.fulfill({ response, body: body }) } catch { @@ -196,8 +187,9 @@ export class Analyser implements AnalyserI { import fss from 'fs' import path from 'path' import { execSync } from 'child_process'; -//@ts-ignore -import { instrument_wasm } from '../wasabi/wasabi_js.js' +import { trimFromLastOccurance } from './test.ts' +import { firefox, webkit, chromium } from 'playwright' +import type { Browser, Frame, Worker, Page} from 'playwright' export type Record = { binary: number[], trace: string }[] @@ -208,8 +200,6 @@ type WasabiRuntime = string[] export default class Benchmark { private record: Record - private constructor() { } - async save(benchmarkPath: string, options) { // await new Promise(resolve => setTimeout(resolve, 10 * 60 * 1000)); if (!fss.existsSync(benchmarkPath)) await fs.mkdir(benchmarkPath, { recursive: true }) @@ -237,14 +227,6 @@ export default class Benchmark { return this.record.map(r => r.binary) } - instrumentBinaries(): WasabiRuntime[] { - return this.record.map((r, i) => { - const { instrumented, js } = instrument_wasm(r.binary) - this.record[i].binary = instrumented - return js - }) - } - getRecord() { return this.record } diff --git a/tests/test-utils.cts b/tests/test-utils.cts deleted file mode 100644 index 01b4df09..00000000 --- a/tests/test-utils.cts +++ /dev/null @@ -1,82 +0,0 @@ -import fs from 'fs/promises' -import path from 'path' - -export async function getDirectoryNames(folderPath: string) { - const entries = await fs.readdir(folderPath, { withFileTypes: true }); - - const directories: string[] = entries - .filter((entry) => entry.isDirectory()) - .map((entry) => entry.name); - - return directories; -} - -export async function delay(ms: number) { - return new Promise(resolve => { - setTimeout(resolve, ms); - }); -} - -export async function rmSafe(path: string) { - try { - await fs.rm(path, { recursive: true }); - } catch { - // file doesnt exist, ok - } -} - - -export function startSpinner(name: string) { - const spinnerChars = ['|', '/', '-', '\\']; - let spinnerIndex = 0; - - process.stdout.write(name + ' '); - return setInterval(() => { - process.stdout.write('\b') - process.stdout.write(spinnerChars[spinnerIndex]); - spinnerIndex = (spinnerIndex + 1) % spinnerChars.length; - }, 130); -} - -export function stopSpinner(interval: NodeJS.Timeout) { - clearInterval(interval); - process.stdout.write('\r') -} - -export async function copyDir(source: string, destination: string) { - await fs.mkdir(destination, { recursive: true }); - const items = await fs.readdir(source, { withFileTypes: true }); - for (const item of items) { - const srcPath = path.join(source, item.name); - const destPath = path.join(destination, item.name); - if (item.isDirectory()) { - await copyDir(srcPath, destPath); - } else { - await fs.copyFile(srcPath, destPath); - } - } -} - -export function trimFromLastOccurance(str: string, substring: string) { - const lastIndex = str.lastIndexOf(substring); - if (lastIndex === -1) { - // Substring not found, return original string or handle as needed - return str; - } - return str.substring(0, lastIndex + substring.length); -} - -export function writeWithSpaces(name: string) { - const totalLength = 45; - if (totalLength < name.length) { - throw new Error("Total length should oe greater than or equal to the length of the initial word."); - } - const spacesLength = totalLength - name.length; - const spaces = " ".repeat(spacesLength); - process.stdout.write(`${name}${spaces}`) -} - -export async function findWatNames(dirPath: string) { - let files = await fs.readdir(dirPath); - return files.filter(f => path.parse(f).ext === ".wat"); -} \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 90cd3eca..17831a1f 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,6 +5,8 @@ "target": "ES2022", "sourceMap": true, "outDir": "./dist", + "noEmit": true, + "allowImportingTsExtensions": true, // "composite": true, }, "include": [