Remove operator overloading (#32)

Part of https://github.com/quickjs-ng/quickjs/issues/17
This commit is contained in:
Ben Noordhuis 2023-11-10 10:23:40 +01:00 committed by GitHub
parent 39e834fc18
commit ae17b8522d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 24 additions and 1111 deletions

View file

@ -381,7 +381,7 @@ examples/point.so: $(OBJDIR)/examples/point.pic.o
############################################################################### ###############################################################################
# documentation # documentation
DOCS=doc/quickjs.pdf doc/quickjs.html doc/jsbignum.pdf doc/jsbignum.html DOCS=doc/quickjs.pdf doc/quickjs.html
build_doc: $(DOCS) build_doc: $(DOCS)
@ -420,17 +420,13 @@ ifndef CONFIG_MINGW
endif endif
ifndef CONFIG_MINGW ifndef CONFIG_MINGW
ifndef CONFIG_DARWIN ifndef CONFIG_DARWIN
ifdef CONFIG_BIGNUM
./qjs --bignum tests/test_bjson.js
else
./qjs tests/test_bjson.js ./qjs tests/test_bjson.js
endif ./qjs tests/test_bjson.js
./qjs examples/test_point.js ./qjs examples/test_point.js
endif endif
endif endif
ifdef CONFIG_BIGNUM ifdef CONFIG_BIGNUM
./qjs --bignum tests/test_op_overloading.js ./qjs tests/test_bignum.js
./qjs --bignum tests/test_bignum.js
endif endif
ifdef CONFIG_M32 ifdef CONFIG_M32
./qjs32 tests/test_closure.js ./qjs32 tests/test_closure.js
@ -440,8 +436,7 @@ ifdef CONFIG_M32
./qjs32 tests/test_std.js ./qjs32 tests/test_std.js
./qjs32 tests/test_worker.js ./qjs32 tests/test_worker.js
ifdef CONFIG_BIGNUM ifdef CONFIG_BIGNUM
./qjs32 --bignum tests/test_op_overloading.js ./qjs32 tests/test_bignum.js
./qjs32 --bignum tests/test_bignum.js
endif endif
endif endif

15
qjs.c
View file

@ -43,9 +43,6 @@
extern const uint8_t qjsc_repl[]; extern const uint8_t qjsc_repl[];
extern const uint32_t qjsc_repl_size; extern const uint32_t qjsc_repl_size;
#ifdef CONFIG_BIGNUM
static int bignum_ext;
#endif
static int eval_buf(JSContext *ctx, const void *buf, int buf_len, static int eval_buf(JSContext *ctx, const void *buf, int buf_len,
const char *filename, int eval_flags) const char *filename, int eval_flags)
@ -107,12 +104,6 @@ static JSContext *JS_NewCustomContext(JSRuntime *rt)
ctx = JS_NewContext(rt); ctx = JS_NewContext(rt);
if (!ctx) if (!ctx)
return NULL; return NULL;
#ifdef CONFIG_BIGNUM
if (bignum_ext) {
JS_AddIntrinsicOperators(ctx);
JS_EnableBignumExt(ctx, TRUE);
}
#endif
/* system modules */ /* system modules */
js_init_module_std(ctx, "std"); js_init_module_std(ctx, "std");
js_init_module_os(ctx, "os"); js_init_module_os(ctx, "os");
@ -392,12 +383,6 @@ int main(int argc, char **argv)
dump_unhandled_promise_rejection = 1; dump_unhandled_promise_rejection = 1;
continue; continue;
} }
#ifdef CONFIG_BIGNUM
if (!strcmp(longopt, "bignum")) {
bignum_ext = 1;
continue;
}
#endif
if (opt == 'q' || !strcmp(longopt, "quit")) { if (opt == 'q' || !strcmp(longopt, "quit")) {
empty_run++; empty_run++;
continue; continue;

25
qjsc.c
View file

@ -360,7 +360,6 @@ void help(void)
{ {
int i; int i;
printf("-flto use link time optimization\n"); printf("-flto use link time optimization\n");
printf("-fbignum enable bignum extensions\n");
printf("-fno-["); printf("-fno-[");
for(i = 0; i < countof(feature_list); i++) { for(i = 0; i < countof(feature_list); i++) {
if (i != 0) if (i != 0)
@ -493,9 +492,6 @@ int main(int argc, char **argv)
int module; int module;
OutputTypeEnum output_type; OutputTypeEnum output_type;
size_t stack_size; size_t stack_size;
#ifdef CONFIG_BIGNUM
BOOL bignum_ext = FALSE;
#endif
namelist_t dynamic_module_list; namelist_t dynamic_module_list;
out_filename = NULL; out_filename = NULL;
@ -548,13 +544,7 @@ int main(int argc, char **argv)
} }
if (i == countof(feature_list)) if (i == countof(feature_list))
goto bad_feature; goto bad_feature;
} else } else {
#ifdef CONFIG_BIGNUM
if (!strcmp(optarg, "bignum")) {
bignum_ext = TRUE;
} else
#endif
{
bad_feature: bad_feature:
fprintf(stderr, "unsupported feature: %s\n", optarg); fprintf(stderr, "unsupported feature: %s\n", optarg);
exit(1); exit(1);
@ -631,12 +621,6 @@ int main(int argc, char **argv)
rt = JS_NewRuntime(); rt = JS_NewRuntime();
ctx = JS_NewContext(rt); ctx = JS_NewContext(rt);
#ifdef CONFIG_BIGNUM
if (bignum_ext) {
JS_AddIntrinsicOperators(ctx);
JS_EnableBignumExt(ctx, TRUE);
}
#endif
/* loader for ES6 modules */ /* loader for ES6 modules */
JS_SetModuleLoaderFunc(rt, NULL, jsc_module_loader, NULL); JS_SetModuleLoaderFunc(rt, NULL, jsc_module_loader, NULL);
@ -685,13 +669,6 @@ int main(int argc, char **argv)
feature_list[i].init_name); feature_list[i].init_name);
} }
} }
#ifdef CONFIG_BIGNUM
if (bignum_ext) {
fprintf(fo,
" JS_AddIntrinsicOperators(ctx);\n"
" JS_EnableBignumExt(ctx, 1);\n");
}
#endif
/* add the precompiled modules (XXX: could modify the module /* add the precompiled modules (XXX: could modify the module
loader instead) */ loader instead) */
for(i = 0; i < init_module_list.count; i++) { for(i = 0; i < init_module_list.count; i++) {

View file

@ -213,8 +213,6 @@ DEF(Float64Array, "Float64Array")
DEF(DataView, "DataView") DEF(DataView, "DataView")
#ifdef CONFIG_BIGNUM #ifdef CONFIG_BIGNUM
DEF(BigInt, "BigInt") DEF(BigInt, "BigInt")
DEF(OperatorSet, "OperatorSet")
DEF(Operators, "Operators")
#endif #endif
DEF(Map, "Map") DEF(Map, "Map")
DEF(Set, "Set") /* Map + 1 */ DEF(Set, "Set") /* Map + 1 */
@ -258,8 +256,5 @@ DEF(Symbol_hasInstance, "Symbol.hasInstance")
DEF(Symbol_species, "Symbol.species") DEF(Symbol_species, "Symbol.species")
DEF(Symbol_unscopables, "Symbol.unscopables") DEF(Symbol_unscopables, "Symbol.unscopables")
DEF(Symbol_asyncIterator, "Symbol.asyncIterator") DEF(Symbol_asyncIterator, "Symbol.asyncIterator")
#ifdef CONFIG_BIGNUM
DEF(Symbol_operatorSet, "Symbol.operatorSet")
#endif
#endif /* DEF */ #endif /* DEF */

865
quickjs.c

File diff suppressed because it is too large Load diff

View file

@ -369,10 +369,6 @@ void JS_AddIntrinsicMapSet(JSContext *ctx);
void JS_AddIntrinsicTypedArrays(JSContext *ctx); void JS_AddIntrinsicTypedArrays(JSContext *ctx);
void JS_AddIntrinsicPromise(JSContext *ctx); void JS_AddIntrinsicPromise(JSContext *ctx);
void JS_AddIntrinsicBigInt(JSContext *ctx); void JS_AddIntrinsicBigInt(JSContext *ctx);
/* enable operator overloading */
void JS_AddIntrinsicOperators(JSContext *ctx);
/* enable "use math" */
void JS_EnableBignumExt(JSContext *ctx, JS_BOOL enable);
JSValue js_string_codePointRange(JSContext *ctx, JSValueConst this_val, JSValue js_string_codePointRange(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv); int argc, JSValueConst *argv);

View file

@ -150,7 +150,6 @@ cp tests/*.js tests/*.patch tests/bjson.c $outdir/tests
cp examples/*.js examples/*.c $outdir/examples cp examples/*.js examples/*.c $outdir/examples
cp doc/quickjs.texi doc/quickjs.pdf doc/quickjs.html \ cp doc/quickjs.texi doc/quickjs.pdf doc/quickjs.html \
doc/jsbignum.texi doc/jsbignum.html doc/jsbignum.pdf \
$outdir/doc $outdir/doc
( cd /tmp && tar Jcvf /tmp/${d}.tar.xz ${d} ) ( cd /tmp && tar Jcvf /tmp/${d}.tar.xz ${d} )

View file

@ -1,207 +0,0 @@
"use strict";
function assert(actual, expected, message) {
if (arguments.length == 1)
expected = true;
if (actual === expected)
return;
if (actual !== null && expected !== null
&& typeof actual == 'object' && typeof expected == 'object'
&& actual.toString() === expected.toString())
return;
throw Error("assertion failed: got |" + actual + "|" +
", expected |" + expected + "|" +
(message ? " (" + message + ")" : ""));
}
/* operators overloading with Operators.create() */
function test_operators_create() {
class Vec2
{
constructor(x, y) {
this.x = x;
this.y = y;
}
static mul_scalar(p1, a) {
var r = new Vec2();
r.x = p1.x * a;
r.y = p1.y * a;
return r;
}
toString() {
return "Vec2(" + this.x + "," + this.y + ")";
}
}
Vec2.prototype[Symbol.operatorSet] = Operators.create(
{
"+"(p1, p2) {
var r = new Vec2();
r.x = p1.x + p2.x;
r.y = p1.y + p2.y;
return r;
},
"-"(p1, p2) {
var r = new Vec2();
r.x = p1.x - p2.x;
r.y = p1.y - p2.y;
return r;
},
"=="(a, b) {
return a.x == b.x && a.y == b.y;
},
"<"(a, b) {
var r;
/* lexicographic order */
if (a.x == b.x)
r = (a.y < b.y);
else
r = (a.x < b.x);
return r;
},
"++"(a) {
var r = new Vec2();
r.x = a.x + 1;
r.y = a.y + 1;
return r;
}
},
{
left: Number,
"*"(a, b) {
return Vec2.mul_scalar(b, a);
}
},
{
right: Number,
"*"(a, b) {
return Vec2.mul_scalar(a, b);
}
});
var a = new Vec2(1, 2);
var b = new Vec2(3, 4);
var r;
r = a * 2 + 3 * b;
assert(r.x === 11 && r.y === 16);
assert(a == a, true);
assert(a == b, false);
assert(a != a, false);
assert(a < b, true);
assert(a <= b, true);
assert(b < a, false);
assert(b <= a, false);
assert(a <= a, true);
assert(a >= a, true);
a++;
assert(a.x === 2 && a.y === 3);
r = ++a;
assert(a.x === 3 && a.y === 4);
assert(r === a);
}
/* operators overloading thru inheritance */
function test_operators()
{
var Vec2;
function mul_scalar(p1, a) {
var r = new Vec2();
r.x = p1.x * a;
r.y = p1.y * a;
return r;
}
var vec2_ops = Operators({
"+"(p1, p2) {
var r = new Vec2();
r.x = p1.x + p2.x;
r.y = p1.y + p2.y;
return r;
},
"-"(p1, p2) {
var r = new Vec2();
r.x = p1.x - p2.x;
r.y = p1.y - p2.y;
return r;
},
"=="(a, b) {
return a.x == b.x && a.y == b.y;
},
"<"(a, b) {
var r;
/* lexicographic order */
if (a.x == b.x)
r = (a.y < b.y);
else
r = (a.x < b.x);
return r;
},
"++"(a) {
var r = new Vec2();
r.x = a.x + 1;
r.y = a.y + 1;
return r;
}
},
{
left: Number,
"*"(a, b) {
return mul_scalar(b, a);
}
},
{
right: Number,
"*"(a, b) {
return mul_scalar(a, b);
}
});
Vec2 = class Vec2 extends vec2_ops
{
constructor(x, y) {
super();
this.x = x;
this.y = y;
}
toString() {
return "Vec2(" + this.x + "," + this.y + ")";
}
}
var a = new Vec2(1, 2);
var b = new Vec2(3, 4);
var r;
r = a * 2 + 3 * b;
assert(r.x === 11 && r.y === 16);
assert(a == a, true);
assert(a == b, false);
assert(a != a, false);
assert(a < b, true);
assert(a <= b, true);
assert(b < a, false);
assert(b <= a, false);
assert(a <= a, true);
assert(a >= a, true);
a++;
assert(a.x === 2 && a.y === 3);
r = ++a;
assert(a.x === 3 && a.y === 4);
assert(r === a);
}
function test_default_op()
{
assert(Object(1) + 2, 3);
assert(Object(1) + true, 2);
assert(-Object(1), -1);
}
test_operators_create();
test_operators();
test_default_op();