-
Notifications
You must be signed in to change notification settings - Fork 19
add regex precompilation to build step #361
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
karthik2804
merged 3 commits into
spinframework:main
from
karthik2804:readd_regex_precompilation
Apr 23, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,15 +3,17 @@ | |
| import { componentize } from '@bytecodealliance/componentize-js'; | ||
| import { version as componentizeVersion } from '@bytecodealliance/componentize-js'; | ||
| import { getPackagesWithWasiDeps, processWasiDeps } from './wasiDepsParser.js'; | ||
|
|
||
| import { | ||
| calculateChecksum, | ||
| saveBuildData, | ||
| } from './utils.js'; | ||
| import { getCliArgs } from './cli.js'; | ||
| import { getBuildDataPath, ShouldComponentize } from './build.js'; | ||
| import { writeFile } from 'node:fs/promises'; | ||
| import { readFile, writeFile } from 'node:fs/promises'; | ||
| import { mergeWit } from '../lib/wit_tools.js'; | ||
| //@ts-ignore | ||
| import { precompile } from "./precompile.js" | ||
| import path from 'node:path' | ||
|
|
||
| async function main() { | ||
| try { | ||
|
|
@@ -61,8 +63,17 @@ async function main() { | |
| 'combined-wit:[email protected]', | ||
| ); | ||
|
|
||
| const source = await readFile(src, 'utf8'); | ||
| const precompiledSource = precompile(source, src, true) as string; | ||
|
|
||
| // Write precompiled source to disk for debugging purposes In the future we | ||
| // will also write a source map to make debugging easier | ||
| let srcDir = path.dirname(src); | ||
| let precompiledSourcePath = path.join(srcDir, 'precompiled-source.js'); | ||
| await writeFile(precompiledSourcePath, precompiledSource); | ||
|
|
||
| const { component } = await componentize({ | ||
| sourcePath: src, | ||
| sourcePath: precompiledSourcePath, | ||
| // @ts-ignore | ||
| witWorld: inlineWit, | ||
| runtimeArgs, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // Copyright 2024 Fastly Inc. | ||
| // License: the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| import regexpuc from 'regexpu-core'; | ||
| import { parse } from 'acorn'; | ||
| import MagicString from 'magic-string'; | ||
| import { simple as simpleWalk } from 'acorn-walk'; | ||
|
|
||
| const PREAMBLE = `;{ | ||
| // Precompiled regular expressions | ||
| const precompile = (r) => { r.exec('a'); r.exec('\\u1000'); };`; | ||
| const POSTAMBLE = '}'; | ||
|
|
||
| /// Emit a block of javascript that will pre-compile the regular expressions given. As spidermonkey | ||
| /// will intern regular expressions, duplicating them at the top level and testing them with both | ||
| /// an ascii and utf8 string should ensure that they won't be re-compiled when run in the fetch | ||
| /// handler. | ||
| export function precompile(source, filename = '<input>', moduleMode = false) { | ||
| const magicString = new MagicString(source, { | ||
| filename, | ||
| }); | ||
|
|
||
| const ast = parse(source, { | ||
| ecmaVersion: 'latest', | ||
| sourceType: moduleMode ? 'module' : 'script', | ||
| }); | ||
|
|
||
| const precompileCalls = []; | ||
| simpleWalk(ast, { | ||
| Literal(node) { | ||
| if (!node.regex) return; | ||
| let transpiledPattern; | ||
| try { | ||
| transpiledPattern = regexpuc(node.regex.pattern, node.regex.flags, { | ||
| unicodePropertyEscapes: 'transform', | ||
| }); | ||
| } catch { | ||
| // swallow regex parse errors here to instead throw them at the engine level | ||
| // this then also avoids regex parser bugs being thrown unnecessarily | ||
| transpiledPattern = node.regex.pattern; | ||
| } | ||
| const transpiledRegex = `/${transpiledPattern}/${node.regex.flags}`; | ||
| precompileCalls.push(`precompile(${transpiledRegex});`); | ||
| magicString.overwrite(node.start, node.end, transpiledRegex); | ||
| }, | ||
| }); | ||
|
|
||
| if (!precompileCalls.length) return source; | ||
|
|
||
| magicString.prepend(`${PREAMBLE}${precompileCalls.join('\n')}${POSTAMBLE}`); | ||
|
|
||
| // When we're ready to pipe in source maps: | ||
| // const map = magicString.generateMap({ | ||
| // source: 'source.js', | ||
| // file: 'converted.js.map', | ||
| // includeContent: true | ||
| // }); | ||
|
|
||
| return magicString.toString(); | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can keep this to the specific process by changing from
kill -9tokill -6. Can you test that and see if it works? I had this result in very unexpected behavior before, and am certain that we (and others) would run into it again in the future.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This did not work, I can take a look at how to fix this in a separate PR.