File tree Expand file tree Collapse file tree 4 files changed +58
-51
lines changed Expand file tree Collapse file tree 4 files changed +58
-51
lines changed Original file line number Diff line number Diff line change
1
+ import Glibc
2
+
3
+ print ( " Hello, 🌐! " )
4
+
5
+ // we can try loops and arithmetic:
6
+
7
+ func fizzBuzz( from: Int , to: Int ) {
8
+ for i in from... to {
9
+ if i % 15 == 0 {
10
+ print ( " FizzBuzz " )
11
+ } else if i % 3 == 0 {
12
+ print ( " Fizz " )
13
+ } else if i % 5 == 0 {
14
+ print ( " Buzz " )
15
+ } else {
16
+ print ( i)
17
+ }
18
+ }
19
+ }
20
+
21
+ fizzBuzz ( from: 1 , to: 20 )
22
+
23
+ func fib( n: Int ) -> Int {
24
+ if n <= 0 { return 0 }
25
+ if n <= 2 { return 1 }
26
+ var a = 1 ;
27
+ var b = 1 ;
28
+ for _ in 3 ... n {
29
+ let newA = b
30
+ let newB = a + b
31
+ a = newA
32
+ b = newB
33
+ }
34
+ return b
35
+ }
36
+
37
+ print ( " The 10th Fibonacci number is \( fib ( n: 10 ) ) " )
38
+
39
+ // we can also run JavaScript from Swift.
40
+
41
+ @_silgen_name ( " executeScript " )
42
+ func executeScript( script: UnsafePointer < UInt8 > , length: Int32 )
43
+
44
+ // Here's a string holding JavaScript code, with some string interpolation:
45
+ var scriptSrc = " alert('Hello from Swift! The 11th Fibonacci number is \( fib ( n: 11 ) ) '); "
46
+ // and we can execute it.
47
+ scriptSrc. withUTF8 { bufferPtr in
48
+ executeScript ( script: bufferPtr. baseAddress!, length: Int32 ( bufferPtr. count) )
49
+ }
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import CodeMirror from "codemirror";
2
2
3
3
import "codemirror/mode/swift/swift" ;
4
4
import "codemirror/lib/codemirror.css" ;
5
+ import kDefaultDemoScript from './demo.swift?raw' ;
5
6
6
7
const kCompileApi = "https://swiftwasm-compiler-api-mgv5x4syda-uc.a.run.app" ;
7
8
const kPrecompiledDemo = true ;
@@ -213,56 +214,5 @@ async function getPrecompiledDemo(): Promise<CompilationResult> {
213
214
} ;
214
215
}
215
216
216
- // Demo script
217
- const kDefaultDemoScript = `import Glibc
218
-
219
- print("Hello, 🌐!")
220
-
221
- // we can try loops and arithmetic:
222
-
223
- func fizzBuzz(from: Int, to: Int) {
224
- for i in from...to {
225
- if i % 15 == 0 {
226
- print("FizzBuzz")
227
- } else if i % 3 == 0 {
228
- print("Fizz")
229
- } else if i % 5 == 0 {
230
- print("Buzz")
231
- } else {
232
- print(i)
233
- }
234
- }
235
- }
236
-
237
- fizzBuzz(from: 1, to: 20)
238
-
239
- func fib(n: Int) -> Int {
240
- if n <= 0 { return 0 }
241
- if n <= 2 { return 1 }
242
- var a = 1;
243
- var b = 1;
244
- for _ in 3...n {
245
- let newA = b
246
- let newB = a + b
247
- a = newA
248
- b = newB
249
- }
250
- return b
251
- }
252
-
253
- print("The 10th Fibonacci number is \\(fib(n: 10))")
254
-
255
- // we can also run JavaScript from Swift.
256
-
257
- @_silgen_name("executeScript")
258
- func executeScript(script: UnsafePointer<UInt8>, length: Int32)
259
-
260
- // Here's a string holding JavaScript code, with some string interpolation:
261
- var scriptSrc = "alert('Hello from Swift! The 11th Fibonacci number is \\(fib(n: 11))');"
262
- // and we can execute it.
263
- scriptSrc.withUTF8 { bufferPtr in
264
- executeScript(script: bufferPtr.baseAddress!, length: Int32(bufferPtr.count))
265
- }
266
- ` ;
267
217
268
218
pageLoaded ( ) ;
Original file line number Diff line number Diff line change
1
+ declare module '*.swift?raw' {
2
+ const value : string ;
3
+ export default value ;
4
+ }
Original file line number Diff line number Diff line change @@ -19,6 +19,10 @@ module.exports = {
19
19
'css-loader' ,
20
20
] ,
21
21
} ,
22
+ {
23
+ resourceQuery : / r a w / ,
24
+ type : 'asset/source' ,
25
+ }
22
26
] ,
23
27
} ,
24
28
resolve : {
You can’t perform that action at this time.
0 commit comments