Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
191 lines (172 loc) · 5.27 KB

File metadata and controls

191 lines (172 loc) · 5.27 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { join } from "node:path"
import { existsSync, watch } from "node:fs"
import {
readFileSync,
writeFileSync,
ensureDirSync,
pathExistsSync,
readJSONSync,
} from "fs-extra"
const debounce = require("debounce-promise")
import {
compile,
jacdacDefaultSpecifications,
DevsDiagnostic,
DEVS_BYTECODE_FILE,
formatDiagnostics,
DEVS_DBG_FILE,
prettySize,
DebugInfo,
parseStackFrame,
} from "@devicescript/compiler"
import { BINDIR, CmdOptions, debug, error, log } from "./command"
import { devtools } from "./devtools"
import type { DevsModule } from "@devicescript/vm"
export function readDebugInfo() {
let dbg: DebugInfo
try {
dbg = readJSONSync(join(BINDIR, DEVS_DBG_FILE))
} catch {}
return dbg
}
let devsInst: DevsModule
export function devsFactory() {
// emscripten doesn't like multiple instances
if (devsInst) return Promise.resolve(devsInst)
const d = require("@devicescript/vm")
try {
require("websocket-polyfill")
// @ts-ignore
global.Blob = require("buffer").Blob
} catch {
log("can't load websocket-polyfill")
}
return (d() as Promise<DevsModule>).then(m => {
devsInst = m
const dbg = readDebugInfo()
if (dbg)
m.dmesg = (s: string) => {
console.debug(parseStackFrame(dbg, s).markedLine)
}
m.devsInit()
return m
})
}
export async function getHost(options: BuildOptions & CmdOptions) {
const inst = options.noVerify ? undefined : await devsFactory()
const outdir = options.outDir || BINDIR
ensureDirSync(outdir)
const devsHost = {
write: (fn: string, cont: string) => {
const p = join(outdir, fn)
if (options.verbose) debug(`write ${p}`)
writeFileSync(p, cont)
if (
fn.endsWith(".jasm") &&
typeof cont == "string" &&
cont.indexOf("???oops") >= 0
)
throw new Error("bad disassembly")
},
log: (msg: string) => {
if (options.verbose) log(msg)
},
error: (err: DevsDiagnostic) => {
error(formatDiagnostics([err]))
},
mainFileName: () => options.mainFileName || "main.ts",
getSpecs: () => jacdacDefaultSpecifications,
verifyBytecode: (buf: Uint8Array) => {
if (!inst) return
const res = inst.devsVerify(buf)
if (res != 0) throw new Error("verification error: " + res)
},
}
return devsHost
}
export class CompilationError extends Error {
constructor(message: string) {
super(message)
this.name = "CompilationError"
}
}
export async function compileFile(fn: string, options: BuildOptions = {}) {
if (!pathExistsSync(fn)) throw new Error(`source file ${fn} not found`)
return compileBuf(readFileSync(fn), { ...options, mainFileName: fn })
}
export async function compileBuf(buf: Buffer, options: BuildOptions = {}) {
const host = await getHost(options)
const res = compile(buf.toString("utf8"), {
host,
isLibrary: options.library,
})
return res
}
export interface BuildOptions {
noVerify?: boolean
library?: boolean
outDir?: string
watch?: boolean
stats?: boolean
// internal option
mainFileName?: string
}
export async function build(file: string, options: BuildOptions & CmdOptions) {
file = file || "main.ts"
options = options || {}
options.outDir = options.outDir || BINDIR
options.mainFileName = file
if (!existsSync(file)) {
// otherwise we throw
error(`${file} does not exist`)
return
}
log(`building ${file}`)
ensureDirSync(options.outDir)
await buildOnce(file, options)
if (options.watch) await buildWatch(file, options)
}
async function buildWatch(file: string, options: BuildOptions) {
const bytecodeFile = join(options.outDir, DEVS_BYTECODE_FILE)
const debugFile = join(options.outDir, DEVS_DBG_FILE)
// start watch source file
log(`watching ${file}...`)
const work = debounce(
async () => {
debug(`change detected...`)
await buildOnce(file, options)
},
500,
{ leading: true }
)
watch(file, work)
// start watching bytecode file
await devtools({ ...options, bytecodeFile, debugFile })
}
async function buildOnce(file: string, options: BuildOptions & CmdOptions) {
const { watch, stats } = options
const { success, binary, dbg } = await compileFile(file, options)
if (!success) {
if (watch) return
throw new CompilationError("compilation failed")
}
log(`bytecode: ${prettySize(binary.length)}`)
if (stats) {
const { sizes, functions } = dbg
log(
" " +
Object.keys(sizes)
.map(name => `${name}: ${prettySize(sizes[name])}`)
.join(", ")
)
log(` functions:`)
functions
.sort((l, r) => l.size - r.size)
.forEach(fn => {
log(` ${fn.name} (${prettySize(fn.size)})`)
fn.users.forEach(user =>
debug(` <-- ${user.file}: ${user.line}, ${user.col}`)
)
})
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.