Skip to content

Commit

Permalink
(add) runtime: generate correct code for identifier-index; implement …
Browse files Browse the repository at this point in the history
…`String.prototype.concat`
  • Loading branch information
xTrayambak committed Dec 18, 2024
1 parent 6c70c54 commit 0356446
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 40 deletions.
8 changes: 6 additions & 2 deletions src/bali/runtime/interpreter.nim
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,8 @@ proc generateIR*(
else:
runtime.ir.returnFn(runtime.index(&stmt.retIdent, defaultParams(fn)).int)
of CallAndStoreResult:
runtime.markLocal(fn, stmt.storeIdent)
runtime.generateIR(fn, stmt.storeFn)
runtime.markLocal(fn, stmt.storeIdent)

let index = runtime.index(stmt.storeIdent, defaultParams(fn))
debug "emitter: call-and-store result will be stored in ident \"" & stmt.storeIdent &
Expand Down Expand Up @@ -692,7 +692,11 @@ proc generateIR*(
runtime.ir.call(normalizeIRName "console.log")
of AccessArrayIndex:
let atomIdx = runtime.index(stmt.arrAccIdent, defaultParams(fn))
let fieldIndex = runtime.loadIRAtom(stmt.arrAccIndex)
let fieldIndex = if *stmt.arrAccIndex:
runtime.loadIRAtom(&stmt.arrAccIndex)
elif *stmt.arrAccIdentIndex:
runtime.index(&stmt.arrAccIdentIndex, defaultParams(fn))
else: unreachable; 0

runtime.ir.passArgument(atomIdx)
runtime.ir.passArgument(fieldIndex)
Expand Down
30 changes: 30 additions & 0 deletions src/bali/stdlib/types/std_string.nim
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,36 @@ proc generateStdIr*(runtime: Runtime) =
ret search.find(value[start ..< value.len], searchStr)
)

runtime.definePrototypeFn(
JSString, "concat",
proc(value: MAtom) =
## 22.1.3.5 String.prototype.concat ( ...args )

# 1. Let O be ? RequireObjectCoercible(this value).
# 2. Let S be ? ToString(O).
let value = runtime.ToString(
runtime.RequireObjectCoercible(&value.tagged("internal"))
)

# 3. Let R be S.
var res = value

# 4. For each element next of args, do
for i in 0 ..< runtime.argumentCount():
# a. Let nextString be ? ToString(next).
let nextString = runtime.ToString(
&runtime.argument(i + 1)
)

# b. Set R to the string-concatenation of R and nextString.
res &= nextString

# 5. Return R.
echo runtime.argumentcount
echo res
ret res
)

runtime.definePrototypeFn(
JSString,
"trim",
Expand Down
9 changes: 9 additions & 0 deletions tests/data/index-ident-001.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var i = 0;
var list = ["A", "B", "C"]

while (i < 2)
{
let val = list[i]
console.log(val)
i++
}
15 changes: 15 additions & 0 deletions tests/data/string-concat-001.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
let x = new String("hello ")
let vals = [
"world",
"nerds",
"folks",
"readers"
]
var i = 0

while (i < 4)
{
let val = vals[i]
console.log(x.concat(val))
i++
}
38 changes: 0 additions & 38 deletions tests/type_nested_object.nim

This file was deleted.

0 comments on commit 0356446

Please sign in to comment.