Skip to content

Commit 8e58dee

Browse files
extract demo script into a separate file
1 parent a44609d commit 8e58dee

File tree

4 files changed

+58
-51
lines changed

4 files changed

+58
-51
lines changed

src/demo.swift

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
}

src/index.ts

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import CodeMirror from "codemirror";
22

33
import "codemirror/mode/swift/swift";
44
import "codemirror/lib/codemirror.css";
5+
import kDefaultDemoScript from './demo.swift?raw';
56

67
const kCompileApi = "https://swiftwasm-compiler-api-mgv5x4syda-uc.a.run.app";
78
const kPrecompiledDemo = true;
@@ -213,56 +214,5 @@ async function getPrecompiledDemo(): Promise<CompilationResult> {
213214
};
214215
}
215216

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-
`;
267217

268218
pageLoaded();

src/types.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare module '*.swift?raw' {
2+
const value: string;
3+
export default value;
4+
}

webpack.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ module.exports = {
1919
'css-loader',
2020
],
2121
},
22+
{
23+
resourceQuery: /raw/,
24+
type: 'asset/source',
25+
}
2226
],
2327
},
2428
resolve: {

0 commit comments

Comments
 (0)