Skip to content

Commit

Permalink
Add JS-OBJECT and JS-ARRAY.
Browse files Browse the repository at this point in the history
  • Loading branch information
manuel committed Mar 21, 2012
1 parent 46ed24a commit 6096749
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
4 changes: 2 additions & 2 deletions repl.virtua
Expand Up @@ -74,13 +74,13 @@
(defun print-stack-trace (f)
;; extreme hack: skip first 6 frames
(def off 6)
(while (> off 0) (set! f (get-slot f "parent")) (set! off (- off 1)))
(while (> off 0) (set! f (get-slot f "parent")) (dec! off))
;; only show fixed number of frames
(def frames 10)
(until (or? (= #void f) (= frames 0))
(print f)
(set! f (get-slot f "parent"))
(set! frames (- frames 1))))
(dec! frames)))

(set! (js-global "lisp_repl_onsubmit") (js-function repl-submit))

Expand Down
6 changes: 6 additions & 0 deletions standard.virtua
Expand Up @@ -502,6 +502,12 @@
(define-js-number-binop "*")
(define-js-number-binop "/"))

(defmacro inc! (var . delta) env
(eval (list set! var (list + var (if (pair? delta) (car delta) 1))) env))

(defmacro dec! (var . delta) env
(eval (list set! var (list - var (if (pair? delta) (car delta) 1))) env))

;;;; Comparison

(def number< (js-binop "<"))
Expand Down
2 changes: 1 addition & 1 deletion test.virtua
Expand Up @@ -36,7 +36,7 @@

;;;; Arrays

(assert (instance? (make-array) Array))
(assert (instance? (js-array) Array))

;;;; Binops

Expand Down
13 changes: 12 additions & 1 deletion virtua.js
Expand Up @@ -26,7 +26,6 @@ function lisp_make_kernel_env() {
lisp_export(env, "null?", lisp_make_wrapped_native(lisp_lib_null, 1, 1));
lisp_export(env, "intern", lisp_make_wrapped_native(lisp_intern, 1, 1));
lisp_export(env, "symbol-name", lisp_make_wrapped_native(lisp_symbol_name, 1, 1));
lisp_export(env, "make-array", lisp_make_wrapped_native(lisp_make_array, 0, 0));
lisp_export(env, "#t", lisp_t);
lisp_export(env, "#f", lisp_f);
lisp_export(env, "#ignore", lisp_ignore);
Expand Down Expand Up @@ -72,6 +71,8 @@ function lisp_make_kernel_env() {
lisp_export(env, "js-call", lisp_make_wrapped_native(lisp_js_call, 2));
lisp_export(env, "js-function", lisp_make_wrapped_native(lisp_js_function, 1, 1));
lisp_export(env, "js-binop", lisp_make_wrapped_native(lisp_js_binop, 1, 1));
lisp_export(env, "js-object", lisp_make_wrapped_native(lisp_js_object, 0, 1));
lisp_export(env, "js-array", lisp_make_wrapped_native(lisp_js_array, 0, 0));
/* Debugging */
lisp_export(env, "stack-frame", lisp_make_wrapped_native(lisp_stack_frame, 0, 0));
lisp_export(env, "Stack-Frame", Lisp_Stack_Frame);
Expand Down Expand Up @@ -386,6 +387,16 @@ function lisp_js_binop(op) {
return cmb;
}

/* Creates JS object with given prototype (optional). */
function lisp_js_object(proto) {
return Object.create((typeof(proto) !== "undefined") ? proto : null);
}

/* Creates JS array. */
function lisp_js_array() {
return [];
}

/**** Arrays ****/

var Lisp_Array = Array.prototype;
Expand Down

0 comments on commit 6096749

Please sign in to comment.