diff options
author | bptato <nincsnevem662@gmail.com> | 2022-12-31 16:47:30 +0100 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2022-12-31 16:47:30 +0100 |
commit | b155fd2a274bd02184d999b8ac186abcbcd4c19a (patch) | |
tree | 8bef0aef36cc351026530f70b839ebaa403234df /src/js | |
parent | a7cc646579489743d5b6ae5b8c6d1646a10710e1 (diff) | |
download | chawan-b155fd2a274bd02184d999b8ac186abcbcd4c19a.tar.gz |
javascript: more robust toJSNumber
Diffstat (limited to 'src/js')
-rw-r--r-- | src/js/javascript.nim | 35 |
1 files changed, 15 insertions, 20 deletions
diff --git a/src/js/javascript.nim b/src/js/javascript.nim index 37e7b017..30ece22d 100644 --- a/src/js/javascript.nim +++ b/src/js/javascript.nim @@ -673,28 +673,23 @@ proc getJSFunction*[T, U](ctx: JSContext, val: JSValue): Option[(proc(x: T): Opt func toJSString(ctx: JSContext, str: string): JSValue = return JS_NewString(ctx, cstring(str)) -func toJSInt(ctx: JSContext, n: SomeInteger): JSValue = - when n is int: - when sizeof(int) <= sizeof(int32): - return JS_NewInt32(ctx, int32(n)) - else: - return JS_NewInt64(ctx, n) - elif n is uint32: - return JS_NewUint32(ctx, n) - elif n is int32: - return JS_NewInt32(ctx, n) - elif n is int64: - return JS_NewInt64(ctx, n) - elif n is uint32: - return JS_NewUint32(ctx, n) +func toJSNumber(ctx: JSContext, n: int): JSValue = + when sizeof(int) <= sizeof(int32): + return JS_NewInt32(ctx, int32(n)) else: - assert false, "Unsupported numeric type" + return JS_NewInt64(ctx, n) -func toJSNumber(ctx: JSContext, n: SomeNumber): JSValue = - when n is SomeInteger: - return toJSInt(ctx, n) - else: - return JS_NewFloat64(ctx, n) +func toJSNumber(ctx: JSContext, n: int32): JSValue = + return JS_NewInt32(ctx, n) + +func toJSNumber(ctx: JSContext, n: int64): JSValue = + return JS_NewInt64(ctx, n) + +func toJSNumber(ctx: JSContext, n: uint32): JSValue = + return JS_NewUint32(ctx, n) + +func toJSNumber[T: float|float32|float64](ctx: JSContext, n: T): JSValue = + return JS_NewFloat64(ctx, n) func toJSBool(ctx: JSContext, b: bool): JSValue = return JS_NewBool(ctx, b) |