Skip to content

Commit

Permalink
(add) fix bug in definePrototypeFn
Browse files Browse the repository at this point in the history
It'd cause a weird memory corruption where deleting GC'd memory would
result in the previously saved atom being completely corrupted. We fix
that by performing a deep copy now.
  • Loading branch information
xTrayambak committed Dec 16, 2024
1 parent bb32650 commit ca68527
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
40 changes: 38 additions & 2 deletions src/balde.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
when not isMainModule:
{.error: "This file is not meant to be separately imported!".}

import std/[strutils, terminal, times, tables, os, monotimes, logging]
import std/[strutils, terminal, times, tables, os, options, monotimes, logging, json]
import bali/grammar/prelude
import bali/internal/sugar
import bali/runtime/prelude
Expand All @@ -15,6 +15,12 @@ import pkg/[colored_logger, jsony, pretty, noise, fuzzy]

const Version {.strdefine: "NimblePkgVersion".} = "<version not defined>"

type
DumpMode = enum
dmPretty
dmJson
dmJsonPretty

var
enableProfiler = false
profile = initTable[string, int64]()
Expand Down Expand Up @@ -74,6 +80,16 @@ proc allocRuntime*(ctx: Input, file: string, ast: AST, repl: bool = false): Runt

runtime

func `%`(t: tuple[str: Option[string], exc: Option[void], ident: Option[string]]): JsonNode =
if *t.str:
return newJString &t.str

if *t.exc:
return "exception".newJString

if *t.ident:
return newJString &t.ident

proc execFile(ctx: Input, file: string) {.inline.} =
profileThis "execFile() sanity checks":
if not fileExists(file):
Expand Down Expand Up @@ -125,7 +141,27 @@ proc execFile(ctx: Input, file: string) {.inline.} =
runtime.run()

if ctx.enabled("dump-ast"):
print ast
var rawMode = ctx.flag("dump-mode")
if !rawMode: rawMode = some("pretty")

let mode = case &rawMode
of "pretty": dmPretty
of "json": dmJson
of "json-pretty": dmJsonPretty
else:
die "invalid mode for dump-mode: " & &rawMode
dmPretty

case mode
of dmPretty: print ast # Pretty-print the AST
else: die "dump mode not implemented"
#[of dmJson:
# convert the AST to JSON using jsony
let serialized = toJson(ast)
echo serialized
of dmJsonPretty:
let serialized = pretty(parseJson(toJson(ast))) # FIXME: this is horribly inefficient but it works
echo serialized]#

if ctx.enabled("dump-runtime-after-exec"):
print runtime
Expand Down
2 changes: 1 addition & 1 deletion src/bali/runtime/bridge.nim
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ proc registerType*[T](runtime: Runtime, name: string, prototype: typedesc[T]) =
let typIdx = runtime.types.len - 1

runtime.vm.registerBuiltin(
"BALI_CONSTRUCTOR_" & name.toUpperAscii(),
"BALI_CONSTRUCTOR_" & strutils.toUpperAscii(name),
proc(_: Operation) =
if runtime.types[typIdx].constructor == nil:
runtime.typeError(runtime.types[typIdx].name & " is not a constructor")
Expand Down
10 changes: 5 additions & 5 deletions src/bali/stdlib/types/std_string.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
## Wraps around the Mirage atom
## Author(s):
## Trayambak Rai (xtrayambak at disroot dot org)
import std/[logging, tables, hashes]
import std/[logging, tables, strutils, hashes]
import bali/runtime/[arguments, bridge, atom_helpers, types]
import bali/runtime/abstract/to_string
import bali/internal/[trim_string, sugar]
import mirage/atom
import std/strutils
import pkg/kaleidoscope/[casings, search]
import pretty

type
Expand Down Expand Up @@ -55,7 +55,7 @@ proc generateStdIr*(runtime: Runtime) =

debug "String.indexOf(): value = \"" & runtime.ToString(value) & "\"; needle = \"" & runtime.ToString(&needle) & '"'

ret find(runtime.ToString(value), runtime.ToString(&needle))
ret search.find(runtime.ToString(value), runtime.ToString(&needle))
)

runtime.definePrototypeFn(
Expand Down Expand Up @@ -116,13 +116,13 @@ proc generateStdIr*(runtime: Runtime) =
proc(value: MAtom) =
let value = &value.tagged("internal")

ret toLowerAscii(runtime.ToString(value))
ret casings.toLowerAscii(runtime.ToString(value))
)

runtime.definePrototypeFn(
JSString, "toUpperCase",
proc(value: MAtom) =
let value = &value.tagged("internal")

ret toUpperAscii(runtime.ToString(value))
ret casings.toUpperAscii(runtime.ToString(value))
)

0 comments on commit ca68527

Please sign in to comment.