reuse jsffi in json module (#17165)

* remove unnecessary when statement

* remove outdated codes

* reuse jsffi

* move js json coverage
This commit is contained in:
flywind 2021-03-05 02:37:12 +08:00 • committed by GitHub
commit 81889fb84c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 23 deletions

View file

@ -908,15 +908,14 @@ proc parseJson*(s: Stream, filename: string = ""; rawIntegers = false, rawFloats
when defined(js): when defined(js):
from std/math import `mod` from std/math import `mod`
type import std/jsffi
JSObject = object
proc parseNativeJson(x: cstring): JSObject {.importc: "JSON.parse".} proc parseNativeJson(x: cstring): JSObject {.importjs: "JSON.parse(#)".}
proc getVarType(x: JSObject): JsonNodeKind = proc getVarType(x: JSObject): JsonNodeKind =
result = JNull result = JNull
proc getProtoName(y: JSObject): cstring proc getProtoName(y: JSObject): cstring
{.importc: "Object.prototype.toString.call".} {.importjs: "Object.prototype.toString.call(#)".}
case $getProtoName(x) # TODO: Implicit returns fail here. case $getProtoName(x) # TODO: Implicit returns fail here.
of "[object Array]": return JArray of "[object Array]": return JArray
of "[object Object]": return JObject of "[object Object]": return JObject
@ -936,18 +935,6 @@ when defined(js):
`result` = `x`.length; `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 = proc convertObject(x: JSObject): JsonNode =
case getVarType(x) case getVarType(x)
of JArray: of JArray:
@ -965,14 +952,14 @@ when defined(js):
result[$nimProperty] = nimValue.convertObject() result[$nimProperty] = nimValue.convertObject()
asm "}}" asm "}}"
of JInt: of JInt:
result = newJInt(cast[int](x)) result = newJInt(x.to(int))
of JFloat: of JFloat:
result = newJFloat(cast[float](x)) result = newJFloat(x.to(float))
of JString: of JString:
# Dunno what to do with isUnquoted here # Dunno what to do with isUnquoted here
result = newJString($cast[cstring](x)) result = newJString($x.to(cstring))
of JBool: of JBool:
result = newJBool(cast[bool](x)) result = newJBool(x.to(bool))
of JNull: of JNull:
result = newJNull() result = newJNull()

View file

@ -1,4 +1,9 @@
import json discard """
targets: "c js"
"""
import std/json
type type
Foo = object Foo = object

View file

@ -1,9 +1,10 @@
discard """ discard """
targets: "c js"
output:'''@["3", "2", "1"] output:'''@["3", "2", "1"]
''' '''
""" """
#12928,10456 #12928,10456
import sequtils, algorithm, json import std/[sequtils, algorithm, json]
proc test() = proc test() =
try: try:

View file

@ -1,4 +1,5 @@
discard """ discard """
targets: "c js"
output: ''' output: '''
Original: (kind: P, pChildren: @[(kind: Text, textStr: "mychild"), (kind: Br)]) Original: (kind: P, pChildren: @[(kind: Text, textStr: "mychild"), (kind: Br)])
jsonNode: {"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 type
ContentNodeKind* = enum ContentNodeKind* = enum