Add new asc compile logic
This commit is contained in:
@@ -13,6 +13,7 @@ import { ref } from 'valtio'
|
||||
* out of it and store both in global state.
|
||||
*/
|
||||
export const compileCode = async (activeId: number) => {
|
||||
let asc: typeof import('assemblyscript/dist/asc') | undefined
|
||||
// Save the file to global state
|
||||
saveFile(false, activeId)
|
||||
if (!process.env.NEXT_PUBLIC_COMPILE_API_ENDPOINT) {
|
||||
@@ -25,6 +26,85 @@ export const compileCode = async (activeId: number) => {
|
||||
}
|
||||
// Set loading state to true
|
||||
state.compiling = true
|
||||
if (typeof window !== 'undefined') {
|
||||
// IF AssemblyScript
|
||||
if (
|
||||
state.files[activeId].language.toLowerCase() === 'ts' ||
|
||||
state.files[activeId].language.toLowerCase() === 'typescript'
|
||||
) {
|
||||
if (!asc) {
|
||||
asc = await import('assemblyscript/dist/asc')
|
||||
}
|
||||
const files: { [key: string]: string } = {}
|
||||
state.files.forEach(file => {
|
||||
files[file.name] = file.content
|
||||
})
|
||||
const res = await asc.main(
|
||||
[
|
||||
state.files[activeId].name,
|
||||
'--textFile',
|
||||
'-o',
|
||||
state.files[activeId].name,
|
||||
'--runtime',
|
||||
'minimal',
|
||||
'-O3'
|
||||
],
|
||||
{
|
||||
readFile: (name, baseDir) => {
|
||||
console.log('--> ', name)
|
||||
const currentFile = state.files.find(file => file.name === name)
|
||||
if (currentFile) {
|
||||
return currentFile.content
|
||||
}
|
||||
return null
|
||||
},
|
||||
writeFile: (name, data, baseDir) => {
|
||||
console.log(name)
|
||||
const curr = state.files.find(file => file.name === name)
|
||||
if (curr) {
|
||||
curr.compiledContent = ref(data)
|
||||
}
|
||||
},
|
||||
listFiles: (dirname, baseDir) => {
|
||||
console.log('listFiles: ' + dirname + ', baseDir=' + baseDir)
|
||||
return []
|
||||
}
|
||||
}
|
||||
)
|
||||
// In case you want to compile just single file
|
||||
// const res = await asc.compileString(state.files[activeId].content, {
|
||||
// optimizeLevel: 3,
|
||||
// runtime: 'stub',
|
||||
// })
|
||||
|
||||
if (res.error?.message) {
|
||||
state.compiling = false
|
||||
state.logs.push({
|
||||
type: 'error',
|
||||
message: res.error.message
|
||||
})
|
||||
state.logs.push({
|
||||
type: 'error',
|
||||
message: res.stderr.toString()
|
||||
})
|
||||
return
|
||||
}
|
||||
if (res.stdout) {
|
||||
const wat = res.stdout.toString()
|
||||
state.files[activeId].lastCompiled = new Date()
|
||||
state.files[activeId].compiledWatContent = wat
|
||||
state.files[activeId].compiledValueSnapshot = state.files[activeId].content
|
||||
state.logs.push({
|
||||
type: 'success',
|
||||
message: `File ${state.files?.[activeId]?.name} compiled successfully. Ready to deploy.`,
|
||||
link: Router.asPath.replace('develop', 'deploy'),
|
||||
linkText: 'Go to deploy'
|
||||
})
|
||||
}
|
||||
state.compiling = false
|
||||
return
|
||||
}
|
||||
}
|
||||
state.logs = []
|
||||
const file = state.files[activeId]
|
||||
try {
|
||||
|
||||
@@ -24,7 +24,7 @@ function toHex(str: string) {
|
||||
return result.toUpperCase()
|
||||
}
|
||||
|
||||
function arrayBufferToHex(arrayBuffer?: ArrayBuffer | null) {
|
||||
function arrayBufferToHex(arrayBuffer?: ArrayBuffer | Uint8Array | null) {
|
||||
if (!arrayBuffer) {
|
||||
return ''
|
||||
}
|
||||
@@ -36,7 +36,7 @@ function arrayBufferToHex(arrayBuffer?: ArrayBuffer | null) {
|
||||
throw new TypeError('Expected input to be an ArrayBuffer')
|
||||
}
|
||||
|
||||
var view = new Uint8Array(arrayBuffer)
|
||||
var view = arrayBuffer instanceof Uint8Array ? arrayBuffer : new Uint8Array(arrayBuffer)
|
||||
var result = ''
|
||||
var value
|
||||
|
||||
|
||||
Reference in New Issue
Block a user