From d471066b08c9c05ff4b2865fc94f069a743fc1db Mon Sep 17 00:00:00 2001 From: flywind Date: Sun, 21 Feb 2021 15:02:09 +0800 Subject: [PATCH 1/8] remove unnecessary when statement --- lib/pure/json.nim | 19 +++++++------------ tests/stdlib/tjson.nim | 11 ++++++++++- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/lib/pure/json.nim b/lib/pure/json.nim index ac3c3b19430d..d610edcbf680 100644 --- a/lib/pure/json.nim +++ b/lib/pure/json.nim @@ -152,9 +152,9 @@ runnableExamples: doAssert $(%* Foo()) == """{"a1":0,"a2":0,"a0":0,"a3":0,"a4":0}""" import - hashes, tables, strutils, lexbase, streams, macros, parsejson + std/[hashes, tables, strutils, lexbase, streams, macros, parsejson] -import options # xxx remove this dependency using same approach as https://github.com/nim-lang/Nim/pull/14563 +import std/options # xxx remove this dependency using same approach as https://github.com/nim-lang/Nim/pull/14563 import std/private/since export @@ -682,13 +682,10 @@ proc toPretty(result: var string, node: JsonNode, indent = 2, ml = true, escapeJson(node.str, result) of JInt: if lstArr: result.indent(currIndent) - when defined(js): result.add($node.num) - else: result.addInt(node.num) + result.addInt(node.num) of JFloat: if lstArr: result.indent(currIndent) - # Fixme: implement new system.add ops for the JS target - when defined(js): result.add($node.fnum) - else: result.addFloat(node.fnum) + result.addFloat(node.fnum) of JBool: if lstArr: result.indent(currIndent) result.add(if node.bval: "true" else: "false") @@ -766,11 +763,9 @@ proc toUgly*(result: var string, node: JsonNode) = else: node.str.escapeJson(result) of JInt: - when defined(js): result.add($node.num) - else: result.addInt(node.num) + result.addInt(node.num) of JFloat: - when defined(js): result.add($node.fnum) - else: result.addFloat(node.fnum) + result.addFloat(node.fnum) of JBool: result.add(if node.bval: "true" else: "false") of JNull: @@ -912,7 +907,7 @@ proc parseJson*(s: Stream, filename: string = ""; rawIntegers = false, rawFloats p.close() when defined(js): - from math import `mod` + from std/math import `mod` type JSObject = object diff --git a/tests/stdlib/tjson.nim b/tests/stdlib/tjson.nim index 78f284abba45..b71961554b92 100644 --- a/tests/stdlib/tjson.nim +++ b/tests/stdlib/tjson.nim @@ -1,3 +1,8 @@ +discard """ + targets: "c cpp js" +""" + + #[ Note: Macro tests are in tests/stdlib/tjsonmacro.nim ]# @@ -234,4 +239,8 @@ doAssert isRefSkipDistinct(MyDistinct) doAssert isRefSkipDistinct(MyOtherDistinct) let x = parseJson("9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999") -doAssert x.kind == JString + +when defined(js): # xxx fixme + doAssert x.kind == JInt +else: + doAssert x.kind == JString From 0824e3df8893769b660a0652700573e2f76919a7 Mon Sep 17 00:00:00 2001 From: flywind Date: Mon, 22 Feb 2021 09:41:57 +0800 Subject: [PATCH 2/8] remove outdated codes --- lib/pure/collections/sequtils.nim | 1 - lib/pure/strtabs.nim | 1 - 2 files changed, 2 deletions(-) diff --git a/lib/pure/collections/sequtils.nim b/lib/pure/collections/sequtils.nim index 70ecff493294..0872257f453b 100644 --- a/lib/pure/collections/sequtils.nim +++ b/lib/pure/collections/sequtils.nim @@ -326,7 +326,6 @@ func distribute*[T](s: seq[T], num: Positive, spread = true): seq[seq[T]] = if num < 2: result = @[s] return - let num = int(num) # XXX probably only needed because of .. bug # Create the result and calculate the stride size and the remainder if any. result = newSeq[seq[T]](num) diff --git a/lib/pure/strtabs.nim b/lib/pure/strtabs.nim index 9047841a6113..3b90fea509e9 100644 --- a/lib/pure/strtabs.nim +++ b/lib/pure/strtabs.nim @@ -300,7 +300,6 @@ proc raiseFormatException(s: string) = proc getValue(t: StringTableRef, flags: set[FormatFlag], key: string): string = if hasKey(t, key): return t.getOrDefault(key) - # hm difficult: assume safety in taint mode here. XXX This is dangerous! when defined(js) or defined(nimscript) or defined(Standalone): result = "" else: From 7820a1eb91b0c31bf4868eec2b68d6ee9bc5b430 Mon Sep 17 00:00:00 2001 From: flywind Date: Wed, 24 Feb 2021 11:12:40 +0800 Subject: [PATCH 3/8] reuse jsffi --- lib/pure/json.nim | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/lib/pure/json.nim b/lib/pure/json.nim index d610edcbf680..c8d1caaddb72 100644 --- a/lib/pure/json.nim +++ b/lib/pure/json.nim @@ -908,15 +908,14 @@ proc parseJson*(s: Stream, filename: string = ""; rawIntegers = false, rawFloats when defined(js): from std/math import `mod` - type - JSObject = object + import std/jsffi - proc parseNativeJson(x: cstring): JSObject {.importc: "JSON.parse".} + proc parseNativeJson(x: cstring): JSObject {.importjs: "JSON.parse(#)".} proc getVarType(x: JSObject): JsonNodeKind = result = JNull proc getProtoName(y: JSObject): cstring - {.importc: "Object.prototype.toString.call".} + {.importjs: "Object.prototype.toString.call(#)".} case $getProtoName(x) # TODO: Implicit returns fail here. of "[object Array]": return JArray of "[object Object]": return JObject @@ -936,18 +935,6 @@ when defined(js): `result` = `x`.length; """ - proc `[]`(x: JSObject, y: string): JSObject = - assert x.getVarType == JObject - asm """ - `result` = `x`[`y`]; - """ - - proc `[]`(x: JSObject, y: int): JSObject = - assert x.getVarType == JArray - asm """ - `result` = `x`[`y`]; - """ - proc convertObject(x: JSObject): JsonNode = case getVarType(x) of JArray: @@ -965,14 +952,14 @@ when defined(js): result[$nimProperty] = nimValue.convertObject() asm "}}" of JInt: - result = newJInt(cast[int](x)) + result = newJInt(x.to(int)) of JFloat: - result = newJFloat(cast[float](x)) + result = newJFloat(x.to(float)) of JString: # Dunno what to do with isUnquoted here - result = newJString($cast[cstring](x)) + result = newJString($x.to(cstring)) of JBool: - result = newJBool(cast[bool](x)) + result = newJBool(x.to(bool)) of JNull: result = newJNull() From e6ee988ad7a8cbe3f5f86f037f855aad066b6535 Mon Sep 17 00:00:00 2001 From: flywind Date: Wed, 24 Feb 2021 16:44:46 +0800 Subject: [PATCH 4/8] move js json coverage --- tests/stdlib/t15835.nim | 7 ++++++- tests/stdlib/talgorithm.nim | 3 ++- tests/stdlib/tjson_unmarshall.nim | 3 ++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/tests/stdlib/t15835.nim b/tests/stdlib/t15835.nim index bddfa87aad1d..ba340578001d 100644 --- a/tests/stdlib/t15835.nim +++ b/tests/stdlib/t15835.nim @@ -1,4 +1,9 @@ -import json +discard """ + targets: "c js" +""" + + +import std/json type Foo = object diff --git a/tests/stdlib/talgorithm.nim b/tests/stdlib/talgorithm.nim index 47a8d327b8b5..fecd858200b9 100644 --- a/tests/stdlib/talgorithm.nim +++ b/tests/stdlib/talgorithm.nim @@ -1,9 +1,10 @@ discard """ + targets: "c js" output:'''@["3", "2", "1"] ''' """ #12928,10456 -import sequtils, algorithm, json +import std/[sequtils, algorithm, json] proc test() = try: diff --git a/tests/stdlib/tjson_unmarshall.nim b/tests/stdlib/tjson_unmarshall.nim index 69bed3ac9b8a..4353d1ee2aa9 100644 --- a/tests/stdlib/tjson_unmarshall.nim +++ b/tests/stdlib/tjson_unmarshall.nim @@ -1,4 +1,5 @@ discard """ + targets: "c js" output: ''' Original: (kind: P, pChildren: @[(kind: Text, textStr: "mychild"), (kind: Br)]) jsonNode: {"kind":"P","pChildren":[{"kind":"Text","textStr":"mychild"},{"kind":"Br"}]} @@ -6,7 +7,7 @@ Reversed: (kind: P, pChildren: @[(kind: Text, textStr: "mychild"), (kind: Br)]) ''' """ -import json +import std/json type ContentNodeKind* = enum From 34e5aced29753bef1cbfd767d450eff819618594 Mon Sep 17 00:00:00 2001 From: flywind Date: Sat, 6 Mar 2021 11:13:11 +0800 Subject: [PATCH 5/8] fix #17267 --- compiler/main.nim | 4 ++++ tests/threads/tjsthreads.nim | 5 +++++ 2 files changed, 9 insertions(+) create mode 100644 tests/threads/tjsthreads.nim diff --git a/compiler/main.nim b/compiler/main.nim index 8959236063a7..a67b5fc6e9cc 100644 --- a/compiler/main.nim +++ b/compiler/main.nim @@ -119,6 +119,10 @@ proc commandCompileToJS(graph: ModuleGraph) = if conf.outFile.isEmpty: conf.outFile = RelativeFile(conf.projectName & ".js") + # js backend will ignore threads options + excl(conf.globalOptions, optThreads) + excl(conf.globalOptions, optThreadAnalysis) + #incl(gGlobalOptions, optSafeCode) setTarget(graph.config.target, osJS, cpuJS) #initDefines() diff --git a/tests/threads/tjsthreads.nim b/tests/threads/tjsthreads.nim new file mode 100644 index 000000000000..d687bc93e0c1 --- /dev/null +++ b/tests/threads/tjsthreads.nim @@ -0,0 +1,5 @@ +discard """ + targets: "js" +""" + +echo 123 From 7aff319d1cbe1f6938ff272cb8648053ddc789b0 Mon Sep 17 00:00:00 2001 From: flywind Date: Sat, 6 Mar 2021 18:51:56 +0800 Subject: [PATCH 6/8] address comments --- compiler/commands.nim | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/compiler/commands.nim b/compiler/commands.nim index 9c6af83d0375..1a79ac2d1728 100644 --- a/compiler/commands.nim +++ b/compiler/commands.nim @@ -613,7 +613,9 @@ proc processSwitch*(switch, arg: string, pass: TCmdLinePass, info: TLineInfo; of "hintaserror": processSpecificNote(arg, wHintAsError, pass, info, switch, conf) of "hints": if processOnOffSwitchOrList(conf, {optHints}, arg, pass, info): listHints(conf) - of "threadanalysis": processOnOffSwitchG(conf, {optThreadAnalysis}, arg, pass, info) + of "threadanalysis": + if conf.backend == backendJs: discard + else: processOnOffSwitchG(conf, {optThreadAnalysis}, arg, pass, info) of "stacktrace": processOnOffSwitch(conf, {optStackTrace}, arg, pass, info) of "stacktracemsgs": processOnOffSwitch(conf, {optStackTraceMsgs}, arg, pass, info) of "excessivestacktrace": processOnOffSwitchG(conf, {optExcessiveStackTrace}, arg, pass, info) @@ -670,7 +672,8 @@ proc processSwitch*(switch, arg: string, pass: TCmdLinePass, info: TLineInfo; of "linedir": processOnOffSwitch(conf, {optLineDir}, arg, pass, info) of "assertions", "a": processOnOffSwitch(conf, {optAssert}, arg, pass, info) of "threads": - processOnOffSwitchG(conf, {optThreads}, arg, pass, info) + if conf.backend == backendJs: discard + else: processOnOffSwitchG(conf, {optThreads}, arg, pass, info) #if optThreads in conf.globalOptions: conf.setNote(warnGcUnsafe) of "tlsemulation": processOnOffSwitchG(conf, {optTlsEmulation}, arg, pass, info) of "implicitstatic": From 951dc2fc7d4ed16d406ee8fcb22f54cf2c49815b Mon Sep 17 00:00:00 2001 From: flywind Date: Sat, 6 Mar 2021 19:54:14 +0800 Subject: [PATCH 7/8] Update compiler/main.nim --- compiler/main.nim | 4 ---- 1 file changed, 4 deletions(-) diff --git a/compiler/main.nim b/compiler/main.nim index a67b5fc6e9cc..8959236063a7 100644 --- a/compiler/main.nim +++ b/compiler/main.nim @@ -119,10 +119,6 @@ proc commandCompileToJS(graph: ModuleGraph) = if conf.outFile.isEmpty: conf.outFile = RelativeFile(conf.projectName & ".js") - # js backend will ignore threads options - excl(conf.globalOptions, optThreads) - excl(conf.globalOptions, optThreadAnalysis) - #incl(gGlobalOptions, optSafeCode) setTarget(graph.config.target, osJS, cpuJS) #initDefines() From 97d326ad64fdaacb3d34fcfed512b912075a47ef Mon Sep 17 00:00:00 2001 From: flywind Date: Sun, 7 Mar 2021 08:44:02 +0800 Subject: [PATCH 8/8] Update tests/threads/tjsthreads.nim --- tests/threads/tjsthreads.nim | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/threads/tjsthreads.nim b/tests/threads/tjsthreads.nim index d687bc93e0c1..1085d9157975 100644 --- a/tests/threads/tjsthreads.nim +++ b/tests/threads/tjsthreads.nim @@ -1,5 +1,6 @@ discard """ - targets: "js" + targets: "c cpp js" + matrix: "--threads" """ echo 123