@@ -3,8 +3,7 @@ import CodeMirror from "codemirror";
33import "codemirror/mode/swift/swift" ;
44import "codemirror/lib/codemirror.css" ;
55
6- const kCompileApi =
7- "https://us-central1-swiftwasm-zhuowei.cloudfunctions.net/compile/v1/compile" ;
6+ const kCompileApi = "https://swiftwasm-compiler-api-mgv5x4syda-uc.a.run.app" ;
87const kPrecompiledDemo = true ;
98
109const kDownloadUrls : {
@@ -22,12 +21,12 @@ var outputArea: HTMLElement | null = null;
2221var downloadWasmButton : HTMLAnchorElement | null = null ;
2322var currentDownloadURL : string | null = null ;
2423
25- interface CompilationResult {
26- output : {
27- success : boolean ;
28- output : string ;
29- } ;
30- binary ?: ArrayBuffer ;
24+ type CompilationResult = {
25+ success : true ,
26+ binary : ArrayBuffer ,
27+ } | {
28+ success : false ;
29+ output : string ;
3130}
3231
3332function writeOutputArea ( text : string ) {
@@ -75,7 +74,7 @@ async function runClicked() {
7574 try {
7675 const compileResult = await compileCode ( code ) ;
7776 populateResultsArea ( compileResult ) ;
78- if ( compileResult . output . success ) {
77+ if ( compileResult . success ) {
7978 runWasm ( compileResult . binary ) ;
8079 }
8180 } catch ( e ) {
@@ -84,43 +83,31 @@ async function runClicked() {
8483 runButton . disabled = false ;
8584}
8685
87- async function compileCode ( code : string ) {
86+ async function compileCode ( code : string ) : Promise < CompilationResult > {
8887 if ( kPrecompiledDemo && code . trim ( ) == kDefaultDemoScript . trim ( ) ) {
8988 return await getPrecompiledDemo ( ) ;
9089 }
9190 const fetchResult = await fetch ( kCompileApi , {
9291 method : "POST" ,
9392 body : JSON . stringify ( {
94- src : code ,
93+ mainCode : code ,
94+ action : "emitExecutable"
9595 } ) ,
9696 headers : {
9797 "Content-Type" : "application/json" ,
9898 } ,
9999 } ) ;
100- const resultBuffer = await fetchResult . arrayBuffer ( ) ;
101- return parseResultBuffer ( resultBuffer ) ;
102- }
103-
104- function parseResultBuffer ( resultBuffer : ArrayBuffer ) : CompilationResult {
105- const textDecoder = new TextDecoder ( "utf-8" ) ;
106- let uint32View = null ;
107- if ( resultBuffer . byteLength >= 8 ) {
108- uint32View = new Uint32Array ( resultBuffer . slice ( 0 , 8 ) ) ;
109- }
110- if ( uint32View == null || uint32View [ 0 ] != 0xdec0ded0 ) {
111- return {
112- output : { success : false , output : textDecoder . decode ( resultBuffer ) } ,
113- } ;
114- }
115- const jsonLength = uint32View [ 1 ] ;
116- const jsonBuffer = resultBuffer . slice ( 8 , 8 + jsonLength ) ;
117- let output : CompilationResult = {
118- output : JSON . parse ( textDecoder . decode ( jsonBuffer ) ) ,
119- } ;
120- if ( 8 + jsonLength < resultBuffer . byteLength ) {
121- output . binary = resultBuffer . slice ( 8 + jsonLength ) ;
100+ if ( fetchResult . ok ) {
101+ const resultBuffer = await fetchResult . arrayBuffer ( ) ;
102+ return { binary : resultBuffer , success : true } ;
103+ } else {
104+ type CompileApiError = {
105+ stderr : string ;
106+ statusCode : number ;
107+ }
108+ const error : CompileApiError = await fetchResult . json ( ) ;
109+ return { success : false , output : error . stderr } ;
122110 }
123- return output ;
124111}
125112
126113async function runWasm ( wasmBuffer : ArrayBuffer ) {
@@ -175,11 +162,15 @@ async function runWasm(wasmBuffer: ArrayBuffer) {
175162
176163function populateResultsArea ( compileResult : CompilationResult ) {
177164 console . log ( compileResult ) ;
178- const output = compileResult . output ;
179- outputArea . textContent = output . output ;
165+ const output = compileResult ;
166+ if ( output . success === false ) {
167+ outputArea . textContent = output . output ;
168+ } else {
169+ outputArea . textContent = "" ;
170+ }
180171 downloadWasmButton . style . display = output . success ? "" : "none" ;
181- if ( compileResult . binary ) {
182- const blob = new Blob ( [ compileResult . binary ] , { type : "application/wasm" } ) ;
172+ if ( output . success ) {
173+ const blob = new Blob ( [ output . binary ] , { type : "application/wasm" } ) ;
183174 currentDownloadURL = URL . createObjectURL ( blob ) ;
184175 downloadWasmButton . href = currentDownloadURL ;
185176 }
@@ -213,14 +204,11 @@ function detectPlatform() {
213204 return "linux" ;
214205}
215206
216- async function getPrecompiledDemo ( ) {
207+ async function getPrecompiledDemo ( ) : Promise < CompilationResult > {
217208 const fetchResult = await fetch ( "/demo_compiled/program.wasm.txt" ) ;
218209 const resultBuffer = await fetchResult . arrayBuffer ( ) ;
219210 return {
220- output : {
221- success : true ,
222- output : "" ,
223- } ,
211+ success : true ,
224212 binary : resultBuffer ,
225213 } ;
226214}
0 commit comments