Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Experimental support for integers as JS bigints #404

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions src/compiler/compiler.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@

(defun literal (sexp &optional recursive)
(cond
((integerp sexp) sexp)
((integerp sexp) `(call-internal |n| ,sexp))
((floatp sexp) sexp)
((characterp sexp) (string sexp))
(t
Expand Down Expand Up @@ -1067,12 +1067,13 @@
(with-collector (fargs)
(with-collector (prelude)
(dolist (x args)
(if (or (floatp x) (numberp x))
(collect-fargs x)
(if (or (floatp x) (integerp x))
(collect-fargs (convert x))
(let ((v (make-symbol (concat "x" (integer-to-string (incf counter))))))
(collect-fargs v)
(collect-prelude `(var (,v ,(convert x))))
(collect-prelude `(if (!= (typeof ,v) "number")
(collect-prelude `(if (and (!= (typeof ,v) "number")
(!= (typeof ,v) "bigint"))
(throw "Not a number!"))))))
`(selfcall
(progn ,@prelude)
Expand Down Expand Up @@ -1155,13 +1156,17 @@
(define-builtin-comparison /= !=)

(define-builtin numberp (x)
(convert-to-bool `(== (typeof ,x) "number")))
(convert-to-bool `(or (== (typeof ,x) "number")
(== (typeof ,x) "bigint"))))

(define-builtin %integer-p (x)
(convert-to-bool `(method-call |Number| "isInteger" ,x)))
(convert-to-bool `(or (method-call |Number| "isInteger" ,x)
(== (typeof ,x) "bigint"))))

(define-builtin %truncate (x)
`(method-call |Math| "trunc" ,x))
`(if (== (typeof ,x) "bigint")
,x
(method-call |Math| "trunc" ,x)))

(define-builtin %floor (x)
`(method-call |Math| "floor" ,x))
Expand All @@ -1170,11 +1175,14 @@
`(method-call |Math| "ceil" ,x))

(define-builtin expt (x y)
`(method-call |Math| "pow" ,x ,y))
`(method-call |Math| "pow" (call |Number| ,x) (call |Number| ,y)))

(define-builtin sqrt (x)
`(method-call |Math| "sqrt" ,x))

(define-builtin float (x)
`(call |Number| ,x))

(define-builtin float-to-string (x)
`(call-internal |make_lisp_string| (method-call ,x |toString|)))

Expand Down Expand Up @@ -1331,14 +1339,14 @@
(define-builtin make-storage-vector (n)
`(selfcall
(var (r #()))
(= (get r "length") ,n)
(= (get r "length") (call |Number| ,n))
(return r)))

(define-builtin storage-vector-size (x)
`(get ,x "length"))
`(call |BigInt| (get ,x "length")))

(define-builtin resize-storage-vector (vector new-size)
`(= (get ,vector "length") ,new-size))
`(= (get ,vector "length") (call |Number| ,new-size)))

(define-builtin storage-vector-ref (vector n)
`(selfcall
Expand Down
6 changes: 6 additions & 0 deletions src/prelude.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ internals.forcemv = function(x) {
};


internals.n = function(n) {
return BigInt(n);
}


//
// Workaround the problems with `new` for arbitrary number of
// arguments. Some primitive constructors (like Date) differ if they
Expand Down Expand Up @@ -195,6 +200,7 @@ internals.char_to_codepoint = function(ch) {
};

internals.char_from_codepoint = function(x) {
x = Number(x)
if (x <= 0xFFFF) {
return String.fromCharCode(x);
} else {
Expand Down