Skip to content

Commit f34c61d

Browse files
committed
FFI libc demo
1 parent 60e5757 commit f34c61d

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

ffi/libc.js

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
"use strict";
2+
/*
3+
* Copyright (c) 2013-2024 Vanessa Freudenberg
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
// This is a minimal libc module for SqueakJS
25+
// serving mostly as a demo for FFI
26+
27+
function libc() {
28+
return {
29+
// LIBC module
30+
getModuleName() { return "libc (SqueakJS)"; },
31+
setInterpreter(proxy) { this.vm = proxy.vm; return true; },
32+
33+
// helper functions
34+
bytesToString(bytes) {
35+
const zero = bytes.indexOf(0);
36+
if (zero >= 0) bytes = bytes.subarray(0, zero);
37+
return String.fromCharCode.apply(null, bytes);
38+
},
39+
stringToBytes(string, bytes) {
40+
for (let i = 0; i < string.length; i++) bytes[i] = string.charCodeAt(i);
41+
bytes[string.length] = 0;
42+
return bytes;
43+
},
44+
45+
// LIBC emulation functions called via FFI
46+
getenv(v) {
47+
v = this.bytesToString(v);
48+
switch (v) {
49+
case "USER": return this.vm.options.user || "squeak";
50+
case "HOME": return this.vm.options.root || "/";
51+
}
52+
this.vm.warnOnce("UNIMPLEMENTED getenv: " + v);
53+
return null;
54+
},
55+
getcwd(buf, size) {
56+
const cwd = this.vm.options.root || "/";
57+
if (!buf) buf = new Uint8Array(cwd.length + 1);
58+
if (size < cwd.length + 1) return 0; // should set errno to ERANGE
59+
this.stringToBytes(cwd, buf);
60+
return buf; // converted to Smalltalk String by FFI if declared as char*
61+
},
62+
};
63+
}
64+
65+
function registerLibC() {
66+
if (typeof Squeak === "object" && Squeak.registerExternalModule) {
67+
Squeak.registerExternalModule('libc', libc());
68+
} else self.setTimeout(registerLibC, 100);
69+
};
70+
71+
registerLibC();

squeak.js

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ import "./plugins/SqueakSSL.js";
7474
import "./plugins/SoundGenerationPlugin.js";
7575
import "./plugins/StarSqueakPlugin.js";
7676
import "./plugins/ZipPlugin.js";
77+
import "./ffi/libc.js";
7778
import "./lib/lz-string.js";
7879
import "./lib/jszip.js";
7980
import "./lib/FileSaver.js";

0 commit comments

Comments
 (0)