Skip to content
This repository was archived by the owner on Dec 8, 2025. It is now read-only.

Commit 62f1a6a

Browse files
committed
patch: send dbg to dev tools
1 parent 3275e2c commit 62f1a6a

File tree

5 files changed

+26
-37
lines changed

5 files changed

+26
-37
lines changed

cli/src/build.ts

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import {
1414
DEVS_BYTECODE_FILE,
1515
formatDiagnostics,
1616
LogInfo,
17+
DEVS_DBG_FILE,
18+
prettySize,
1719
} from "devicescript-compiler"
1820
import { BINDIR, CmdOptions, debug, error, log } from "./command"
1921
import { devtools } from "./devtools"
@@ -113,6 +115,7 @@ export async function build(file: string, options: BuildOptions & CmdOptions) {
113115

114116
async function buildWatch(file: string, options: BuildOptions) {
115117
const bytecodeFile = join(options.outDir, DEVS_BYTECODE_FILE)
118+
const debugFile = join(options.outDir, DEVS_DBG_FILE)
116119

117120
// start watch source file
118121
log(`watching ${file}...`)
@@ -127,32 +130,7 @@ async function buildWatch(file: string, options: BuildOptions) {
127130
watch(file, work)
128131

129132
// start watching bytecode file
130-
await devtools({ ...options, bytecodeFile })
131-
}
132-
133-
function roundWithPrecision(
134-
x: number,
135-
digits: number,
136-
round = Math.round
137-
): number {
138-
digits = digits | 0
139-
// invalid digits input
140-
if (digits <= 0) return round(x)
141-
if (x == 0) return 0
142-
let r = 0
143-
while (r == 0 && digits < 21) {
144-
const d = Math.pow(10, digits++)
145-
r = round(x * d + Number.EPSILON) / d
146-
}
147-
return r
148-
}
149-
function prettySize(b: number) {
150-
b = b | 0
151-
if (b === 0) return "0kb"
152-
else if (b < 100) return b + "b"
153-
else if (b < 1000) return roundWithPrecision(b / 1e3, 2) + "kb"
154-
else if (b < 1000000) return roundWithPrecision(b / 1e3, 1) + "kb"
155-
else return roundWithPrecision(b / 1e6, 1) + "mb"
133+
await devtools({ ...options, bytecodeFile, debugFile })
156134
}
157135

158136
async function buildOnce(file: string, options: BuildOptions & CmdOptions) {

cli/src/devtools.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import http from "http"
55
import https from "https"
66
import url from "url"
77
import net from "net"
8-
import fs from "fs"
98
import { CmdOptions, debug, error, log } from "./command"
9+
import { readFileSync, readJSONSync, watch } from "fs-extra"
10+
import { prettySize } from "devicescript-compiler"
1011

1112
const dasboardPath = "tools/devicescript-devtools"
1213

@@ -46,10 +47,11 @@ export interface DevToolsOptions {
4647
localhost?: boolean
4748

4849
bytecodeFile?: string
50+
debugFile?: string
4951
}
5052

5153
export async function devtools(options: DevToolsOptions & CmdOptions) {
52-
const { internet, localhost, bytecodeFile } = options
54+
const { internet, localhost, bytecodeFile, debugFile } = options
5355
const port = 8081
5456
const tcpPort = 8082
5557
const listenHost = internet ? undefined : "127.0.0.1"
@@ -67,16 +69,16 @@ export async function devtools(options: DevToolsOptions & CmdOptions) {
6769
// upload DeviceScript file is needed
6870
const sendDeviceScript = bytecodeFile
6971
? () => {
70-
const bytecode = fs.readFileSync(bytecodeFile)
72+
const bytecode = readFileSync(bytecodeFile)
73+
const dbg = debugFile ? readJSONSync(debugFile) : undefined
7174
debug(
72-
`refresh bytecode ${Math.round(
73-
(bytecode.length || 0) / 1000
74-
)}kb...`
75+
`refresh bytecode ${prettySize(bytecode.length)}...`
7576
)
7677
const msg = JSON.stringify({
7778
type: "bytecode",
7879
channel: "devicescript",
7980
bytecode: bytecode.toString("hex"),
81+
dbg,
8082
})
8183
clients.forEach(c => c.send(msg))
8284
}
@@ -146,7 +148,7 @@ export async function devtools(options: DevToolsOptions & CmdOptions) {
146148
tcpServer.listen(tcpPort, listenHost)
147149

148150
if (bytecodeFile) {
149-
debug(`watch ${bytecodeFile}`)
150-
fs.watch(bytecodeFile, sendDeviceScript)
151+
debug(`watching ${bytecodeFile}...`)
152+
watch(bytecodeFile, sendDeviceScript)
151153
}
152154
}

compiler/src/compiler.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2871,7 +2871,6 @@ class Program implements TopOpWriter {
28712871
this.host.write(DEVS_ASSEMBLY_FILE, this.getAssembly())
28722872

28732873
const { binary, dbg } = this.serialize()
2874-
this.host.write(DEVS_BYTECODE_FILE, binary)
28752874
const progJson = {
28762875
text: this._source,
28772876
blocks: "",
@@ -2880,10 +2879,10 @@ class Program implements TopOpWriter {
28802879
this.host.write(DEVS_BODY_FILE, JSON.stringify(progJson, null, 4))
28812880
this.host.write(DEVS_DBG_FILE, JSON.stringify(dbg, null, 4))
28822881
this.host.write(DEVS_SIZES_FILE, computeSizes(dbg))
2883-
2884-
// write assembly again
28852882
if (this.numErrors == 0)
28862883
this.host.write(DEVS_ASSEMBLY_FILE, this.getAssembly())
2884+
// this file is tracked by --watch and should be written last
2885+
this.host.write(DEVS_BYTECODE_FILE, binary)
28872886

28882887
if (this.numErrors == 0) {
28892888
try {

compiler/src/devicescript.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export * from "./specgen"
66
export * from "./embedspecs"
77
export * from "./tsiface"
88
export * from "./info"
9+
export { prettySize } from "./jdutil"
910

1011
import { compile } from "./compiler"
1112

compiler/src/jdutil.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,15 @@ export function renderWithPrecision(
564564
return rs
565565
}
566566

567+
export function prettySize(b: number) {
568+
b = b | 0
569+
if (b === 0) return "0kb"
570+
else if (b < 100) return b + "b"
571+
else if (b < 1000) return roundWithPrecision(b / 1e3, 2) + "kb"
572+
else if (b < 1000000) return roundWithPrecision(b / 1e3, 1) + "kb"
573+
else return roundWithPrecision(b / 1e6, 1) + "mb"
574+
}
575+
567576
export function randomRange(min: number, max: number) {
568577
return Math.round(Math.random() * (max - min) + min)
569578
}

0 commit comments

Comments
 (0)