JS: Fixes math and streams module and implements json.
This commit is contained in:
parent
b0a16fb619
commit
96c97d4103
3 changed files with 128 additions and 55 deletions
|
|
@ -861,6 +861,7 @@ proc parseJson(p: var TJsonParser): PJsonNode =
|
||||||
of tkError, tkCurlyRi, tkBracketRi, tkColon, tkComma, tkEof:
|
of tkError, tkCurlyRi, tkBracketRi, tkColon, tkComma, tkEof:
|
||||||
raiseParseErr(p, "{")
|
raiseParseErr(p, "{")
|
||||||
|
|
||||||
|
when not defined(js):
|
||||||
proc parseJson*(s: PStream, filename: string): PJsonNode =
|
proc parseJson*(s: PStream, filename: string): PJsonNode =
|
||||||
## Parses from a stream `s` into a `PJsonNode`. `filename` is only needed
|
## Parses from a stream `s` into a `PJsonNode`. `filename` is only needed
|
||||||
## for nice error messages.
|
## for nice error messages.
|
||||||
|
|
@ -880,6 +881,76 @@ proc parseFile*(filename: string): PJsonNode =
|
||||||
if stream == nil:
|
if stream == nil:
|
||||||
raise newException(EIO, "cannot read from file: " & filename)
|
raise newException(EIO, "cannot read from file: " & filename)
|
||||||
result = parseJson(stream, filename)
|
result = parseJson(stream, filename)
|
||||||
|
else:
|
||||||
|
from math import `mod`
|
||||||
|
type
|
||||||
|
TJSObject = object
|
||||||
|
proc parseNativeJson(x: cstring): TJSObject {.importc: "JSON.parse".}
|
||||||
|
|
||||||
|
proc getVarType(x): TJsonNodeKind =
|
||||||
|
result = JNull
|
||||||
|
proc getProtoName(y): cstring
|
||||||
|
{.importc: "Object.prototype.toString.call".}
|
||||||
|
case $getProtoName(x) # TODO: Implicit returns fail here.
|
||||||
|
of "[object Array]": return JArray
|
||||||
|
of "[object Object]": return JObject
|
||||||
|
of "[object Number]":
|
||||||
|
if cast[float](x) mod 1.0 == 0:
|
||||||
|
return JInt
|
||||||
|
else:
|
||||||
|
return JFloat
|
||||||
|
of "[object Boolean]": return JBool
|
||||||
|
of "[object Null]": return JNull
|
||||||
|
of "[object String]": return JString
|
||||||
|
else: assert false
|
||||||
|
|
||||||
|
proc len(x: TJSObject): int =
|
||||||
|
assert x.getVarType == JArray
|
||||||
|
asm """
|
||||||
|
return `x`.length;
|
||||||
|
"""
|
||||||
|
|
||||||
|
proc `[]`(x: TJSObject, y: string): TJSObject =
|
||||||
|
assert x.getVarType == JObject
|
||||||
|
asm """
|
||||||
|
return `x`[`y`];
|
||||||
|
"""
|
||||||
|
|
||||||
|
proc `[]`(x: TJSObject, y: int): TJSObject =
|
||||||
|
assert x.getVarType == JArray
|
||||||
|
asm """
|
||||||
|
return `x`[`y`];
|
||||||
|
"""
|
||||||
|
|
||||||
|
proc convertObject(x: TJSObject): PJsonNode =
|
||||||
|
case getVarType(x)
|
||||||
|
of JArray:
|
||||||
|
result = newJArray()
|
||||||
|
for i in 0 .. <x.len:
|
||||||
|
result.add(x[i].convertObject())
|
||||||
|
of JObject:
|
||||||
|
result = newJObject()
|
||||||
|
asm """for (property in `x`) {
|
||||||
|
if (`x`.hasOwnProperty(property)) {
|
||||||
|
"""
|
||||||
|
var nimProperty: cstring
|
||||||
|
var nimValue: TJSObject
|
||||||
|
asm "`nimProperty` = property; `nimValue` = `x`[property];"
|
||||||
|
result[$nimProperty] = nimValue.convertObject()
|
||||||
|
asm "}}"
|
||||||
|
of JInt:
|
||||||
|
result = newJInt(cast[int](x))
|
||||||
|
of JFloat:
|
||||||
|
result = newJFloat(cast[float](x))
|
||||||
|
of JString:
|
||||||
|
result = newJString($cast[cstring](x))
|
||||||
|
of JBool:
|
||||||
|
result = newJBool(cast[bool](x))
|
||||||
|
of JNull:
|
||||||
|
result = newJNull()
|
||||||
|
|
||||||
|
proc parseJson*(buffer: string): PJsonNode =
|
||||||
|
return parseNativeJson(buffer).convertObject()
|
||||||
|
|
||||||
when false:
|
when false:
|
||||||
import os
|
import os
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@
|
||||||
|
|
||||||
when defined(Posix) and not defined(haiku):
|
when defined(Posix) and not defined(haiku):
|
||||||
{.passl: "-lm".}
|
{.passl: "-lm".}
|
||||||
|
when not defined(js):
|
||||||
import times
|
import times
|
||||||
|
|
||||||
const
|
const
|
||||||
|
|
|
||||||
|
|
@ -239,6 +239,8 @@ proc newStringStream*(s: string = ""): PStringStream =
|
||||||
result.readDataImpl = ssReadData
|
result.readDataImpl = ssReadData
|
||||||
result.writeDataImpl = ssWriteData
|
result.writeDataImpl = ssWriteData
|
||||||
|
|
||||||
|
when not defined(js):
|
||||||
|
|
||||||
type
|
type
|
||||||
PFileStream* = ref TFileStream ## a stream that encapsulates a `TFile`
|
PFileStream* = ref TFileStream ## a stream that encapsulates a `TFile`
|
||||||
TFileStream* = object of TStream
|
TFileStream* = object of TStream
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue