diff options
Diffstat (limited to 'src/io')
| -rw-r--r-- | src/io/console.nim | 25 | ||||
| -rw-r--r-- | src/io/dynstream.nim | 36 | ||||
| -rw-r--r-- | src/io/timeout.nim | 9 |
3 files changed, 23 insertions, 47 deletions
diff --git a/src/io/console.nim b/src/io/console.nim index 87260353..25a989a9 100644 --- a/src/io/console.nim +++ b/src/io/console.nim @@ -1,18 +1,17 @@ -import io/dynstream import monoucha/fromjs import monoucha/javascript import types/opt import utils/twtstr type Console* = ref object - err*: DynStream + err: File clearFun: proc() showFun: proc() hideFun: proc() jsDestructor(Console) -proc newConsole*(err: DynStream; clearFun: proc() = nil; showFun: proc() = nil; +proc newConsole*(err: File; clearFun: proc() = nil; showFun: proc() = nil; hideFun: proc() = nil): Console = return Console( err: err, @@ -21,6 +20,17 @@ proc newConsole*(err: DynStream; clearFun: proc() = nil; showFun: proc() = nil; hideFun: hideFun ) +proc setStream*(console: Console; file: File) = + console.err.close() + console.err = file + +proc write*(console: Console; c: char) = + discard console.err.writeBuffer(unsafeAddr c, 1) + +proc write*(console: Console; s: openArray[char]) = + if s.len > 0: + discard console.err.writeBuffer(unsafeAddr s[0], s.len) + proc log*(console: Console; ss: varargs[string]) = var buf = "" for i, s in ss.mypairs: @@ -80,6 +90,9 @@ proc addConsoleModule*(ctx: JSContext) = # "For historical reasons, console is lowercased." ctx.registerType(Console, nointerface = true, name = "console") -proc writeException*(ctx: JSContext; s: DynStream) = - s.write(ctx.getExceptionMsg()) - discard s.flush() +proc flush*(console: Console) = + console.err.flushFile() + +proc writeException*(console: Console; ctx: JSContext) = + console.err.write(ctx.getExceptionMsg()) + console.flush() diff --git a/src/io/dynstream.nim b/src/io/dynstream.nim index 5fce2bfe..f6bc4418 100644 --- a/src/io/dynstream.nim +++ b/src/io/dynstream.nim @@ -447,39 +447,3 @@ method flush*(s: BufStream): bool = proc newBufStream*(s: SocketStream; registerFun: proc(fd: int)): BufStream = return BufStream(source: s, registerFun: registerFun) - -type - DynFileStream* = ref object of DynStream - file*: File - -method readData*(s: DynFileStream; buffer: pointer; len: int): int = - let n = s.file.readBuffer(buffer, len) - if n == 0: - s.setEnd() - return n - -method writeData*(s: DynFileStream; buffer: pointer; len: int): int = - return s.file.writeBuffer(buffer, len) - -method seek*(s: DynFileStream; off: int64): int64 = - s.file.setFilePos(off) - return off - -method sclose*(s: DynFileStream) = - assert not s.closed - s.file.close() - s.closed = true - -proc c_fflush(f: File): cint {.importc: "fflush", header: "<stdio.h>".} - -method flush*(s: DynFileStream): bool = - return c_fflush(s.file) == 0 - -proc newDynFileStream*(file: File): DynFileStream = - return DynFileStream(file: file) - -proc newDynFileStream*(path: string): DynFileStream = - var file: File = nil - if file.open(path): - return newDynFileStream(path) - return nil diff --git a/src/io/timeout.nim b/src/io/timeout.nim index 0015d892..c131b9c6 100644 --- a/src/io/timeout.nim +++ b/src/io/timeout.nim @@ -2,7 +2,6 @@ import std/algorithm import std/times import io/console -import io/dynstream import monoucha/fromjs import monoucha/javascript import monoucha/jsutils @@ -80,12 +79,12 @@ proc setTimeout*(state: var TimeoutState; t: TimeoutType; handler: JSValue; state.sorted = false return id -proc runEntry(state: var TimeoutState; entry: TimeoutEntry; err: DynStream) = +proc runEntry(state: var TimeoutState; entry: TimeoutEntry; console: Console) = if JS_IsFunction(state.jsctx, entry.val): let ret = JS_Call(state.jsctx, entry.val, JS_UNDEFINED, cint(entry.args.len), entry.args.toJSValueArray()) if JS_IsException(ret): - state.jsctx.writeException(err) + console.writeException(state.jsctx) JS_FreeValue(state.jsctx, ret) else: var s: string @@ -103,7 +102,7 @@ proc sortAndGetTimeout*(state: var TimeoutState): cint = let now = getUnixMillis() return cint(max(state.timeouts[^1].expires - now, -1)) -proc run*(state: var TimeoutState; err: DynStream): bool = +proc run*(state: var TimeoutState; console: Console): bool = let now = getUnixMillis() var found = false var H = state.timeouts.high @@ -113,7 +112,7 @@ proc run*(state: var TimeoutState; err: DynStream): bool = let entry = state.timeouts[i] if entry.dead: continue - state.runEntry(entry, err) + state.runEntry(entry, console) found = true case entry.t of ttTimeout: |