Skip to content

Commit

Permalink
inline DefaultScript to Script
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Apr 21, 2024
1 parent b3d84aa commit e182a0b
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 43 deletions.
2 changes: 0 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
inline DefaultScript to Script

尝试用 node:test 而不用 test-runner

- 因为每次开启 node 解释器有严重的 overhead。
Expand Down
4 changes: 2 additions & 2 deletions src/lang/globals/GlobalStore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Loader } from "../../loader"
import * as Scripts from "../../scripts"
import { Script } from "../../script"
import { envEntries, envExtend } from "../env"
import { Mod } from "../mod"
import type { Value } from "../value"
Expand All @@ -15,7 +15,7 @@ export class GlobalStore {
}

code(text: string): void {
const script = Scripts.createScript(this.mod, text)
const script = new Script(this.mod, text)
script.run()
}

Expand Down
5 changes: 2 additions & 3 deletions src/loader/Loader.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { FetcherSync } from "@cicada-lang/framework/lib/fetcher-sync"
import { Mod } from "../lang/mod"
import type { Script } from "../script"
import * as Scripts from "../scripts"
import { Script } from "../script"

export interface LoaderOptions {
onOutput?: (output: string) => void
Expand All @@ -21,7 +20,7 @@ export class Loader {
this.tracked.push(url)
const text = options?.text || this.fetcher.fetch(url)
const mod = new Mod({ url, loader: this })
const script = Scripts.createScript(mod, text)
const script = new Script(mod, text)
script.run()
this.cache.set(url.href, script)
return mod
Expand Down
21 changes: 17 additions & 4 deletions src/script/Script.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import { createErrorReport } from "../lang/errors"
import { executeStmts } from "../lang/execute"
import type { Mod } from "../lang/mod"
import { parseStmts } from "../lang/syntax"

export abstract class Script {
abstract mod: Mod
abstract text: string
abstract run(): void
export class Script {
constructor(
public mod: Mod,
public text: string,
) {}

run(): void {
try {
const stmts = parseStmts(this.text)
executeStmts(this.mod, stmts)
} catch (error) {
throw createErrorReport(error, this.text)
}
}
}
23 changes: 0 additions & 23 deletions src/scripts/DefaultScript.ts

This file was deleted.

7 changes: 0 additions & 7 deletions src/scripts/createScript.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/scripts/index.ts

This file was deleted.

0 comments on commit e182a0b

Please sign in to comment.