Skip to content

Commit 775ae3d

Browse files
committed
Add new asc compile logic
1 parent 6fa68b9 commit 775ae3d

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

state/actions/compileCode.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { ref } from 'valtio'
1313
* out of it and store both in global state.
1414
*/
1515
export const compileCode = async (activeId: number) => {
16+
let asc: typeof import('assemblyscript/dist/asc') | undefined
1617
// Save the file to global state
1718
saveFile(false, activeId)
1819
if (!process.env.NEXT_PUBLIC_COMPILE_API_ENDPOINT) {
@@ -25,6 +26,85 @@ export const compileCode = async (activeId: number) => {
2526
}
2627
// Set loading state to true
2728
state.compiling = true
29+
if (typeof window !== 'undefined') {
30+
// IF AssemblyScript
31+
if (
32+
state.files[activeId].language.toLowerCase() === 'ts' ||
33+
state.files[activeId].language.toLowerCase() === 'typescript'
34+
) {
35+
if (!asc) {
36+
asc = await import('assemblyscript/dist/asc')
37+
}
38+
const files: { [key: string]: string } = {}
39+
state.files.forEach(file => {
40+
files[file.name] = file.content
41+
})
42+
const res = await asc.main(
43+
[
44+
state.files[activeId].name,
45+
'--textFile',
46+
'-o',
47+
state.files[activeId].name,
48+
'--runtime',
49+
'minimal',
50+
'-O3'
51+
],
52+
{
53+
readFile: (name, baseDir) => {
54+
console.log('--> ', name)
55+
const currentFile = state.files.find(file => file.name === name)
56+
if (currentFile) {
57+
return currentFile.content
58+
}
59+
return null
60+
},
61+
writeFile: (name, data, baseDir) => {
62+
console.log(name)
63+
const curr = state.files.find(file => file.name === name)
64+
if (curr) {
65+
curr.compiledContent = ref(data)
66+
}
67+
},
68+
listFiles: (dirname, baseDir) => {
69+
console.log('listFiles: ' + dirname + ', baseDir=' + baseDir)
70+
return []
71+
}
72+
}
73+
)
74+
// In case you want to compile just single file
75+
// const res = await asc.compileString(state.files[activeId].content, {
76+
// optimizeLevel: 3,
77+
// runtime: 'stub',
78+
// })
79+
80+
if (res.error?.message) {
81+
state.compiling = false
82+
state.logs.push({
83+
type: 'error',
84+
message: res.error.message
85+
})
86+
state.logs.push({
87+
type: 'error',
88+
message: res.stderr.toString()
89+
})
90+
return
91+
}
92+
if (res.stdout) {
93+
const wat = res.stdout.toString()
94+
state.files[activeId].lastCompiled = new Date()
95+
state.files[activeId].compiledWatContent = wat
96+
state.files[activeId].compiledValueSnapshot = state.files[activeId].content
97+
state.logs.push({
98+
type: 'success',
99+
message: `File ${state.files?.[activeId]?.name} compiled successfully. Ready to deploy.`,
100+
link: Router.asPath.replace('develop', 'deploy'),
101+
linkText: 'Go to deploy'
102+
})
103+
}
104+
state.compiling = false
105+
return
106+
}
107+
}
28108
state.logs = []
29109
const file = state.files[activeId]
30110
try {

state/actions/deployHook.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function toHex(str: string) {
2424
return result.toUpperCase()
2525
}
2626

27-
function arrayBufferToHex(arrayBuffer?: ArrayBuffer | null) {
27+
function arrayBufferToHex(arrayBuffer?: ArrayBuffer | Uint8Array | null) {
2828
if (!arrayBuffer) {
2929
return ''
3030
}
@@ -36,7 +36,7 @@ function arrayBufferToHex(arrayBuffer?: ArrayBuffer | null) {
3636
throw new TypeError('Expected input to be an ArrayBuffer')
3737
}
3838

39-
var view = new Uint8Array(arrayBuffer)
39+
var view = arrayBuffer instanceof Uint8Array ? arrayBuffer : new Uint8Array(arrayBuffer)
4040
var result = ''
4141
var value
4242

0 commit comments

Comments
 (0)