Re:Compiling Dynamic Languages (2013) [pdf]
by lispm on Sunday April 14, 2019 @09:23AM
(#19659268)
> you need to box the variables (carry type information in addition to the value).
Dealing with boxing/unboxing and trying to minimize boxing operations in computations is regularly done in Lisp compilers.
> It is possible to introduce new variables in the global scope from a local scope
just like in Lisp. Generally Javascript and Lisp have a lot in common. Scoping rules are different though and Lisp is not object-oriented at the core - but provides closures or adds object-oriented extensions like CLOS which are semi-optional.
* (defun f ()
(setf a 5))
; in: DEFUN F
; (SETF A 5)
; ==>
; (SETQ A 5)
;
; caught WARNING:
; undefined variable: COMMON-LISP-USER::A
;
; compilation unit finished
; Undefined variable:
; A
; caught 1 WARNING condition
F
* (defun g ()
(print a))
; in: DEFUN G
; (PRINT A)
;
; caught WARNING:
; undefined variable: COMMON-LISP-USER::A
;
; compilation unit finished
; Undefined variable:
; A
; caught 1 WARNING condition
As you can see the compiler warns about undefined variables, but deals with it.
* (if (> (random 1.0) 0.5) (f))
5
* (g)
5
5
If we remove the binding of A, then we get a runtime error.
* (makunbound 'a)
A
* (g)
debugger invoked on a UNBOUND-VARIABLE in thread
#<THREAD "main thread" RUNNING {10005205B3}>:
The variable A is unbound.
Type HELP for debugger help, or (SB-EXT:EXIT) to exit from SBCL.
restarts (invokable by number or by possibly-abbreviated name):
0: [CONTINUE ] Retry using A.
1: [USE-VALUE ] Use specified value.
2: [STORE-VALUE] Set specified value and use it.
3: [ABORT ] Exit debugger, returning to top level.
(G)
source: (PRINT A)
0]
As one can see, both F and G are actually compiled machine code functions. Both functions we directly AOT compiled to machine code when I entered them at the prompt.
Dealing with boxing/unboxing and trying to minimize boxing operations in computations is regularly done in Lisp compilers.
> It is possible to introduce new variables in the global scope from a local scope
just like in Lisp. Generally Javascript and Lisp have a lot in common. Scoping rules are different though and Lisp is not object-oriented at the core - but provides closures or adds object-oriented extensions like CLOS which are semi-optional.
As you can see the compiler warns about undefined variables, but deals with it. If we remove the binding of A, then we get a runtime error. As one can see, both F and G are actually compiled machine code functions. Both functions we directly AOT compiled to machine code when I entered them at the prompt.