-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathHost.v3
More file actions
48 lines (42 loc) · 1.32 KB
/
Host.v3
File metadata and controls
48 lines (42 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright 2020 Ben L. Titzer. All rights reserved.
// See LICENSE for details of Apache 2.0 license.
// Host objects must extend this class to be extern refs.
class HostObject extends Object {
def render(buf: StringBuilder) -> StringBuilder {
return buf.puts("<hostobj>");
}
}
// Host functions must extend this class to be func refs.
type HostFunc(sig: SigDecl, invoke: Range<Value> -> HostResult) #unboxed;
class HostFunction extends Function {
def name: string;
def invoke: Range<Value> -> HostResult;
new(name, sig: SigDecl, invoke) super(sig) { }
def render(buf: StringBuilder) -> StringBuilder {
return if(name != null, buf.put1("<hostfunc: %s>", name), buf.puts("<hostfunc>"));
}
}
// The possible return values from calling a host function.
type HostResult {
case Throw(thrown: Throwable);
case Value0;
case Value1(val: Value);
case ValueN(vals: Array<Value>);
case TailCall(func: Function, args: Array<Value>);
}
// Host types must extend this class to be importable.
class HostType extends ExportedType {
def isAssignableTo(t: ValueType) -> bool {
match (t) {
Host(host) => return this == host;
Ref(nullable, heap) => match (heap) {
EXTERN => return true;
_ => return false;
}
_ => return false;
}
}
def render(buf: StringBuilder) -> StringBuilder {
return buf.puts("<hosttype>");
}
}