This commit is contained in:
2026-01-28 21:59:04 -05:00
parent dcd516d970
commit ffcb6f658b
10 changed files with 60 additions and 134 deletions

View File

@@ -41,17 +41,16 @@ func New(ctx context.Context, opts ...RuntimeOption) (*Runtime, error) {
}
func (r *Runtime) populateGlobals() error {
// Register Custom Functions
for name, fn := range functions.GetRegisteredFunctions() {
// Register Main Function
if fn.IsAsync() {
r.ctx.SetAsyncFunc(name, func(this *qjs.This) {
qjsVal, err := callFunc(this, fn)
if err != nil {
this.Promise().Reject(this.Context().NewError(err))
_ = this.Promise().Reject(this.Context().NewError(err))
return
}
this.Promise().Resolve(qjsVal)
_ = this.Promise().Resolve(qjsVal)
})
} else {
r.ctx.SetFunc(name, func(this *qjs.This) (*qjs.Value, error) {

View File

@@ -3,6 +3,7 @@ package runtime
import (
"bytes"
"context"
"os"
"strings"
"testing"
@@ -26,7 +27,44 @@ func TestExecuteTypeScript(t *testing.T) {
rt, err := New(context.Background(), WithStderr(&stderr), WithStdout(&stdout))
assert.NoError(t, err, "Expected no error")
err = rt.RunFile("../../test_data/test.ts")
tsCode := `interface Person {
name: string;
age: number;
email: string;
}
function greet(person: Person): string {
return "Hello, " + person.name + "! You are " + person.age + " years old.";
}
const user: Person = {
name: "Alice",
age: 30,
email: "alice@example.com",
};
console.log(greet(user));
console.log("Email: " + user.email);
function calculateSum(a: number, b: number): number {
return a + b;
}
console.log("Sum of 5 and 10 is: " + calculateSum(5, 10));
`
tmpFile, err := os.CreateTemp("", "*.ts")
assert.NoError(t, err, "Failed to create temp file")
t.Cleanup(func() {
_ = os.Remove(tmpFile.Name())
})
_, err = tmpFile.WriteString(tsCode)
assert.NoError(t, err, "Failed to write to temp file")
err = tmpFile.Close()
assert.NoError(t, err, "Failed to close temp file")
err = rt.RunFile(tmpFile.Name())
assert.NoError(t, err, "Expected no error")
assert.Empty(t, stderr.String(), "Expected no error output")