From df9d09960aad774427e6e3eb27b86b13884ec8dd Mon Sep 17 00:00:00 2001 From: Shawn Presser Date: Mon, 17 Sep 2018 17:11:47 -0500 Subject: [PATCH] Define INSTANCEOF; get rid of TYPE on JS --- bin/compiler.js | 13 ++++++++++++- bin/compiler.lua | 13 ++++++++++++- bin/lumen.js | 33 +++++++++++++++------------------ compiler.l | 9 ++++++++- runtime.l | 14 ++++++-------- test.l | 2 ++ 6 files changed, 55 insertions(+), 29 deletions(-) diff --git a/bin/compiler.js b/bin/compiler.js index 5fc68f2..03108aa 100644 --- a/bin/compiler.js +++ b/bin/compiler.js @@ -1225,7 +1225,18 @@ setenv("new", {_stash: true, special: function (x) { return "new " + compile(x); }}); setenv("typeof", {_stash: true, special: function (x) { - return "typeof(" + compile(x) + ")"; + if (target === "lua") { + return compile(["type", x]); + } else { + return "typeof(" + compile(x) + ")"; + } +}}); +setenv("instanceof", {_stash: true, special: function (x, t) { + if (target === "lua") { + return compile(["=", ["getmetatable", x], t]); + } else { + return "((" + compile(x) + ") instanceof " + compile(t) + ")"; + } }}); setenv("throw", {_stash: true, special: function (x) { var __e56 = undefined; diff --git a/bin/compiler.lua b/bin/compiler.lua index c1ba3f0..73decb7 100644 --- a/bin/compiler.lua +++ b/bin/compiler.lua @@ -1178,7 +1178,18 @@ setenv("new", {_stash = true, special = function (x) return "new " .. compile(x) end}) setenv("typeof", {_stash = true, special = function (x) - return "typeof(" .. compile(x) .. ")" + if target == "lua" then + return compile({"type", x}) + else + return "typeof(" .. compile(x) .. ")" + end +end}) +setenv("instanceof", {_stash = true, special = function (x, t) + if target == "lua" then + return compile({"=", {"getmetatable", x}, t}) + else + return "((" .. compile(x) .. ") instanceof " .. compile(t) .. ")" + end end}) setenv("throw", {_stash = true, special = function (x) local __e48 = nil diff --git a/bin/lumen.js b/bin/lumen.js index f760831..af1e06e 100644 --- a/bin/lumen.js +++ b/bin/lumen.js @@ -40,23 +40,20 @@ two63 = function (x) { hd = function (l) { return l[0]; }; -type = function (x) { - return typeof(x); -}; string63 = function (x) { - return type(x) === "string"; + return typeof(x) === "string"; }; number63 = function (x) { - return type(x) === "number"; + return typeof(x) === "number"; }; boolean63 = function (x) { - return type(x) === "boolean"; + return typeof(x) === "boolean"; }; function63 = function (x) { - return type(x) === "function"; + return typeof(x) === "function"; }; obj63 = function (x) { - return is63(x) && type(x) === "object"; + return is63(x) && typeof(x) === "object"; }; atom63 = function (x) { return nil63(x) || string63(x) || number63(x) || boolean63(x); @@ -191,13 +188,13 @@ reduce = function (f, x) { }; join = function () { var __ls = unstash(Array.prototype.slice.call(arguments, 0)); - var __r38 = []; + var __r37 = []; var ____x1 = __ls; var ____i4 = 0; while (____i4 < _35(____x1)) { var __l11 = ____x1[____i4]; if (__l11) { - var __n3 = _35(__r38); + var __n3 = _35(__r37); var ____o2 = __l11; var __k4 = undefined; for (__k4 in ____o2) { @@ -212,12 +209,12 @@ join = function () { if (number63(__k5)) { __k5 = __k5 + __n3; } - __r38[__k5] = __v2; + __r37[__k5] = __v2; } } ____i4 = ____i4 + 1; } - return __r38; + return __r37; }; find = function (f, t) { var ____o3 = t; @@ -674,16 +671,16 @@ apply = function (f, args) { return f.apply(f, __args); }; call = function (f) { - var ____r75 = unstash(Array.prototype.slice.call(arguments, 1)); - var __f = destash33(f, ____r75); - var ____id = ____r75; + var ____r74 = unstash(Array.prototype.slice.call(arguments, 1)); + var __f = destash33(f, ____r74); + var ____id = ____r74; var __args11 = cut(____id, 0); return apply(__f, __args11); }; setenv = function (k) { - var ____r76 = unstash(Array.prototype.slice.call(arguments, 1)); - var __k18 = destash33(k, ____r76); - var ____id1 = ____r76; + var ____r75 = unstash(Array.prototype.slice.call(arguments, 1)); + var __k18 = destash33(k, ____r75); + var ____id1 = ____r75; var __keys = cut(____id1, 0); if (string63(__k18)) { var __e19 = undefined; diff --git a/compiler.l b/compiler.l index de2bbbd..a464949 100644 --- a/compiler.l +++ b/compiler.l @@ -676,7 +676,14 @@ (cat "new " (compile x))) (define-special typeof (x) - (cat "typeof(" (compile x) ")")) + (if (= target 'lua) + (compile `(type ,x)) + (cat "typeof(" (compile x) ")"))) + +(define-special instanceof (x t) + (if (= target 'lua) + (compile `(= (getmetatable ,x) ,t)) + (cat "((" (compile x) ") instanceof " (compile t) ")"))) (define-special throw (x) :stmt (let e (if (= target 'js) diff --git a/runtime.l b/runtime.l index 50951c8..4ecac2d 100644 --- a/runtime.l +++ b/runtime.l @@ -26,16 +26,14 @@ (define-global hd (l) (at l 0)) -(target js: (define-global type (x) (typeof x))) - -(define-global string? (x) (= (type x) 'string)) -(define-global number? (x) (= (type x) 'number)) -(define-global boolean? (x) (= (type x) 'boolean)) -(define-global function? (x) (= (type x) 'function)) +(define-global string? (x) (= (typeof x) 'string)) +(define-global number? (x) (= (typeof x) 'number)) +(define-global boolean? (x) (= (typeof x) 'boolean)) +(define-global function? (x) (= (typeof x) 'function)) (define-global obj? (x) (and (is? x) - (= (type x) (target lua: 'table js: 'object)))) + (= (typeof x) (target lua: 'table js: 'object)))) (define-global atom? (x) (or (nil? x) (string? x) (number? x) (boolean? x))) @@ -302,7 +300,7 @@ (atom? x) (tostring x) (function? x) "function" (and stack (in? x stack)) "circular" - (target js: false lua: (not (= (type x) 'table))) + (target js: false lua: (not (= (typeof x) 'table))) (escape (tostring x)) (let (s "(" sp "" xs () ks () diff --git a/test.l b/test.l index 80b089a..a55cf65 100755 --- a/test.l +++ b/test.l @@ -598,6 +598,8 @@ c" (test= 1 ((fn () (return (if true (return 1) 2)))))) (define-test guard + (let ((ok e) (guard (error "foo"))) + (test= true (target js: (instanceof e Error) lua: (obj? e)))) (let-macro ((guard1 (x) (let-unique (ok v) `(let ((,ok ,v) (guard ,x))