Merge branches/quickjs to trunk. This is the way.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3621 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
145
deps/quickjs/repl.js
vendored
145
deps/quickjs/repl.js
vendored
@@ -1,8 +1,8 @@
|
||||
/*
|
||||
* QuickJS Read Eval Print Loop
|
||||
*
|
||||
* Copyright (c) 2017-2019 Fabrice Bellard
|
||||
* Copyright (c) 2017-2019 Charlie Gordon
|
||||
* Copyright (c) 2017-2020 Fabrice Bellard
|
||||
* Copyright (c) 2017-2020 Charlie Gordon
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
@@ -209,6 +209,29 @@ import * as os from "os";
|
||||
(is_alpha(c) || is_digit(c) || c == '_' || c == '$');
|
||||
}
|
||||
|
||||
function ucs_length(str) {
|
||||
var len, c, i, str_len = str.length;
|
||||
len = 0;
|
||||
/* we never count the trailing surrogate to have the
|
||||
following property: ucs_length(str) =
|
||||
ucs_length(str.substring(0, a)) + ucs_length(str.substring(a,
|
||||
str.length)) for 0 <= a <= str.length */
|
||||
for(i = 0; i < str_len; i++) {
|
||||
c = str.charCodeAt(i);
|
||||
if (c < 0xdc00 || c >= 0xe000)
|
||||
len++;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
function is_trailing_surrogate(c) {
|
||||
var d;
|
||||
if (typeof c !== "string")
|
||||
return false;
|
||||
d = c.codePointAt(0); /* can be NaN if empty string */
|
||||
return d >= 0xdc00 && d < 0xe000;
|
||||
}
|
||||
|
||||
function is_balanced(a, b) {
|
||||
switch (a + b) {
|
||||
case "()":
|
||||
@@ -235,6 +258,7 @@ import * as os from "os";
|
||||
std.puts("\x1b[" + ((n != 1) ? n : "") + code);
|
||||
}
|
||||
|
||||
/* XXX: handle double-width characters */
|
||||
function move_cursor(delta) {
|
||||
var i, l;
|
||||
if (delta > 0) {
|
||||
@@ -269,15 +293,16 @@ import * as os from "os";
|
||||
}
|
||||
|
||||
function update() {
|
||||
var i;
|
||||
|
||||
var i, cmd_len;
|
||||
/* cursor_pos is the position in 16 bit characters inside the
|
||||
UTF-16 string 'cmd' */
|
||||
if (cmd != last_cmd) {
|
||||
if (!show_colors && last_cmd.substring(0, last_cursor_pos) == cmd.substring(0, last_cursor_pos)) {
|
||||
/* optimize common case */
|
||||
std.puts(cmd.substring(last_cursor_pos));
|
||||
} else {
|
||||
/* goto the start of the line */
|
||||
move_cursor(-last_cursor_pos);
|
||||
move_cursor(-ucs_length(last_cmd.substring(0, last_cursor_pos)));
|
||||
if (show_colors) {
|
||||
var str = mexpr ? mexpr + '\n' + cmd : cmd;
|
||||
var start = str.length - cmd.length;
|
||||
@@ -287,8 +312,7 @@ import * as os from "os";
|
||||
std.puts(cmd);
|
||||
}
|
||||
}
|
||||
/* Note: assuming no surrogate pairs */
|
||||
term_cursor_x = (term_cursor_x + cmd.length) % term_width;
|
||||
term_cursor_x = (term_cursor_x + ucs_length(cmd)) % term_width;
|
||||
if (term_cursor_x == 0) {
|
||||
/* show the cursor on the next line */
|
||||
std.puts(" \x08");
|
||||
@@ -298,7 +322,11 @@ import * as os from "os";
|
||||
last_cmd = cmd;
|
||||
last_cursor_pos = cmd.length;
|
||||
}
|
||||
move_cursor(cursor_pos - last_cursor_pos);
|
||||
if (cursor_pos > last_cursor_pos) {
|
||||
move_cursor(ucs_length(cmd.substring(last_cursor_pos, cursor_pos)));
|
||||
} else if (cursor_pos < last_cursor_pos) {
|
||||
move_cursor(-ucs_length(cmd.substring(cursor_pos, last_cursor_pos)));
|
||||
}
|
||||
last_cursor_pos = cursor_pos;
|
||||
std.out.flush();
|
||||
}
|
||||
@@ -333,13 +361,19 @@ import * as os from "os";
|
||||
}
|
||||
|
||||
function forward_char() {
|
||||
if (cursor_pos < cmd.length)
|
||||
if (cursor_pos < cmd.length) {
|
||||
cursor_pos++;
|
||||
while (is_trailing_surrogate(cmd.charAt(cursor_pos)))
|
||||
cursor_pos++;
|
||||
}
|
||||
}
|
||||
|
||||
function backward_char() {
|
||||
if (cursor_pos > 0)
|
||||
if (cursor_pos > 0) {
|
||||
cursor_pos--;
|
||||
while (is_trailing_surrogate(cmd.charAt(cursor_pos)))
|
||||
cursor_pos--;
|
||||
}
|
||||
}
|
||||
|
||||
function skip_word_forward(pos) {
|
||||
@@ -419,8 +453,18 @@ import * as os from "os";
|
||||
}
|
||||
|
||||
function delete_char_dir(dir) {
|
||||
var start = cursor_pos - (dir < 0);
|
||||
var end = start + 1;
|
||||
var start, end;
|
||||
|
||||
start = cursor_pos;
|
||||
if (dir < 0) {
|
||||
start--;
|
||||
while (is_trailing_surrogate(cmd.charAt(start)))
|
||||
start--;
|
||||
}
|
||||
end = start + 1;
|
||||
while (is_trailing_surrogate(cmd.charAt(end)))
|
||||
end++;
|
||||
|
||||
if (start >= 0 && start < cmd.length) {
|
||||
if (last_fun === kill_region) {
|
||||
kill_region(start, end, dir);
|
||||
@@ -752,7 +796,7 @@ import * as os from "os";
|
||||
function readline_print_prompt()
|
||||
{
|
||||
std.puts(prompt);
|
||||
term_cursor_x = prompt.length % term_width;
|
||||
term_cursor_x = ucs_length(prompt) % term_width;
|
||||
last_cmd = "";
|
||||
last_cursor_pos = 0;
|
||||
}
|
||||
@@ -785,7 +829,7 @@ import * as os from "os";
|
||||
|
||||
function handle_char(c1) {
|
||||
var c;
|
||||
c = String.fromCharCode(c1);
|
||||
c = String.fromCodePoint(c1);
|
||||
switch(readline_state) {
|
||||
case 0:
|
||||
if (c == '\x1b') { /* '^[' - ESC */
|
||||
@@ -825,7 +869,7 @@ import * as os from "os";
|
||||
var fun;
|
||||
|
||||
if (quote_flag) {
|
||||
if (keys.length === 1)
|
||||
if (ucs_length(keys) === 1)
|
||||
insert(keys);
|
||||
quote_flag = false;
|
||||
} else if (fun = commands[keys]) {
|
||||
@@ -845,7 +889,7 @@ import * as os from "os";
|
||||
return;
|
||||
}
|
||||
last_fun = this_fun;
|
||||
} else if (keys.length === 1 && keys >= ' ') {
|
||||
} else if (ucs_length(keys) === 1 && keys >= ' ') {
|
||||
insert(keys);
|
||||
last_fun = insert;
|
||||
} else {
|
||||
@@ -860,23 +904,40 @@ import * as os from "os";
|
||||
var hex_mode = false;
|
||||
var eval_mode = "std";
|
||||
|
||||
function bignum_typeof(a) {
|
||||
"use bigint";
|
||||
return typeof a;
|
||||
}
|
||||
|
||||
function eval_mode_typeof(a) {
|
||||
if (eval_mode === "std")
|
||||
return typeof a;
|
||||
else
|
||||
return bignum_typeof(a);
|
||||
}
|
||||
|
||||
function number_to_string(a, radix) {
|
||||
var s;
|
||||
if (!isFinite(a)) {
|
||||
/* NaN, Infinite */
|
||||
if (typeof a === "bigfloat" && eval_mode !== "math") {
|
||||
return a.toString();
|
||||
} else {
|
||||
if (a == 0) {
|
||||
if (1 / a < 0)
|
||||
s = "-0";
|
||||
else
|
||||
s = "0";
|
||||
} else {
|
||||
if (radix == 16 && a === Math.floor(a)) {
|
||||
var s;
|
||||
if (a < 0) {
|
||||
a = -a;
|
||||
s = "-";
|
||||
} else {
|
||||
s = "";
|
||||
}
|
||||
s += "0x" + a.toString(16);
|
||||
} else {
|
||||
s = a.toString();
|
||||
}
|
||||
}
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
function bigfloat_to_string(a, radix) {
|
||||
var s;
|
||||
if (!BigFloat.isFinite(a)) {
|
||||
/* NaN, Infinite */
|
||||
if (eval_mode !== "math") {
|
||||
return "BigFloat(" + a.toString() + ")";
|
||||
} else {
|
||||
return a.toString();
|
||||
@@ -939,7 +1000,7 @@ import * as os from "os";
|
||||
function print_rec(a) {
|
||||
var n, i, keys, key, type, s;
|
||||
|
||||
type = eval_mode_typeof(a);
|
||||
type = typeof(a);
|
||||
if (type === "object") {
|
||||
if (a === null) {
|
||||
std.puts(a);
|
||||
@@ -994,10 +1055,14 @@ import * as os from "os";
|
||||
if (s.length > 79)
|
||||
s = s.substring(0, 75) + "...\"";
|
||||
std.puts(s);
|
||||
} else if (type === "number" || type === "bigfloat") {
|
||||
} else if (type === "number") {
|
||||
std.puts(number_to_string(a, hex_mode ? 16 : 10));
|
||||
} else if (type === "bigint") {
|
||||
std.puts(bigint_to_string(a, hex_mode ? 16 : 10));
|
||||
} else if (type === "bigfloat") {
|
||||
std.puts(bigfloat_to_string(a, hex_mode ? 16 : 10));
|
||||
} else if (type === "bigdecimal") {
|
||||
std.puts(a.toString() + "m");
|
||||
} else if (type === "symbol") {
|
||||
std.puts(String(a));
|
||||
} else if (type === "function") {
|
||||
@@ -1093,8 +1158,7 @@ import * as os from "os";
|
||||
param = expr.substring(cmd.length + 1).trim();
|
||||
if (param === "") {
|
||||
std.puts("Running mode=" + eval_mode + "\n");
|
||||
} else if (param === "std" || param === "math" ||
|
||||
param === "bigint") {
|
||||
} else if (param === "std" || param === "math") {
|
||||
eval_mode = param;
|
||||
} else {
|
||||
std.puts("Invalid mode\n");
|
||||
@@ -1145,14 +1209,14 @@ import * as os from "os";
|
||||
"\\t " + sel(show_time) + "toggle timing display\n" +
|
||||
"\\clear clear the terminal\n");
|
||||
if (has_jscalc) {
|
||||
std.puts("\\a " + sel(algebraicMode) + "algegraic mode\n" +
|
||||
std.puts("\\a " + sel(algebraicMode) + "algebraic mode\n" +
|
||||
"\\n " + sel(!algebraicMode) + "numeric mode\n");
|
||||
}
|
||||
if (has_bignum) {
|
||||
std.puts("\\p [m [e]] set the BigFloat precision to 'm' bits\n" +
|
||||
"\\digits n set the BigFloat precision to 'ceil(n*log2(10))' bits\n");
|
||||
if (!has_jscalc) {
|
||||
std.puts("\\mode [std|bigint|math] change the running mode (current = " + eval_mode + ")\n");
|
||||
std.puts("\\mode [std|math] change the running mode (current = " + eval_mode + ")\n");
|
||||
}
|
||||
}
|
||||
if (!config_numcalc) {
|
||||
@@ -1166,11 +1230,9 @@ import * as os from "os";
|
||||
try {
|
||||
if (eval_mode === "math")
|
||||
expr = '"use math"; void 0;' + expr;
|
||||
else if (eval_mode === "bigint")
|
||||
expr = '"use bigint"; void 0;' + expr;
|
||||
var now = (new Date).getTime();
|
||||
/* eval as a script */
|
||||
result = std.evalScript(expr);
|
||||
result = std.evalScript(expr, { backtrace_barrier: true });
|
||||
eval_time = (new Date).getTime() - now;
|
||||
std.puts(colors[styles.result]);
|
||||
print(result);
|
||||
@@ -1202,15 +1264,12 @@ import * as os from "os";
|
||||
}
|
||||
if (has_bignum) {
|
||||
log2_10 = Math.log(10) / Math.log(2);
|
||||
prec = 113;
|
||||
expBits = 15;
|
||||
if (has_jscalc) {
|
||||
prec = 113;
|
||||
expBits = 15;
|
||||
eval_mode = "math";
|
||||
/* XXX: numeric mode should always be the default ? */
|
||||
g.algebraicMode = config_numcalc;
|
||||
} else {
|
||||
prec = 53;
|
||||
expBits = 11;
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user