-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlanguage
66 lines (50 loc) · 2.53 KB
/
language
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
Extensions to the Scheme language for sinjs.
(foreign-inline CODE ARG ...)
CODE is a format string using exactly as many ~a elements as there are
ARGs. When the args are substituted into the CODE, the result should be
a valid JavaScript expression evaluating to a possible Scheme object.
CODE must be a literal string. ARG may be any Scheme expression.
Examples (without argument type checking):
(define (car p)
(foreign-inline "~a.car" p))
(define (sin x)
(foreign-inline "Math.sin(~a)" (number->jsnum x)))
(foreign-prologue CODE)
CODE is added to the prologue for the current library or main program,
and will be executed by the JavaScript system before any Scheme code
(from this or any other library) is executed.
===not yet===
(foreign-lambda (ARG ...) CODE)
Returns a procedure whose source code is in Javascript. The ARG ...
parameters must be a list of identifiers in legitimate JavaScript
syntax (only letters, digits, and _ allowed), and CODE must be a literal
string. Write as if the body of a normal JavaScript function. Use
"return" to specify a return value. For example:
((foreign-lambda (x y) "var a = x+y; if (x > 0) return a; else return 0;")
3 5)
=> 8
(foreign-lambda-cps (CONT ARG ...) CODE)
Just like foreign-lambda, but the procedure is called in primitive
continuation-passing style. The parameter CONT must be called with
the return value of the function.
For example this does the same as the preceding example:
((foreign-primitive (k x y)
"var a=x+y; if (x>0) return k(a); else return k(0);")
3 5)
=> 8
Scheme procedures have a different calling structure from normal JavaScript
functions, so you should not directly call a Scheme procedure from JavaScript.
(You may be tempted to do so, because a Scheme procedure is implemented as
a JavaScript object, but please don't! The calling semantic is tricky.)
Instead, use the following wrapper, which makes sure to honor the Scheme
stack rules:
callScheme(proc, arg ...)
This calls a Scheme procedure with the specified arguments. The Scheme
procedure must not return more than once or an error will be raised:
implicit JavaScript continuations cannot be invoked more than once, so
if PROC saves its continuation and executes it a second time, there will be
no way to invoke callScheme's continuation a second time.
(number->jsnum NUMBER)
Returns a JavaScript number for the Scheme NUMBER. It is an error if
NUMBER is not real. If NUMBER is too big to fit in a JavaScript number,
an appropriate NaN might be returned.