From 0d43ec8971f0644fe108b1327899a3ba9c7295b1 Mon Sep 17 00:00:00 2001 From: "John J. Camilleri" Date: Thu, 7 Oct 2021 23:25:25 +0200 Subject: [PATCH] Unmarshalling for floats and strings, but strings crashes after multiple invocations --- src/runtime/javascript/ffi.ts | 15 +++++++--- src/runtime/javascript/tests/basic.test.ts | 34 +++++++++++++++++++++- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/runtime/javascript/ffi.ts b/src/runtime/javascript/ffi.ts index 582cf9928..89df95197 100644 --- a/src/runtime/javascript/ffi.ts +++ b/src/runtime/javascript/ffi.ts @@ -236,12 +236,19 @@ const lint = ffi.Callback(PgfLiteral, [PgfUnmarshaller, ref.types.size_t, ref.re const lflt = ffi.Callback(PgfLiteral, [PgfUnmarshaller, ref.types.double], function (self, val: number): Pointer { - return 0 as any + const obj = new Literal(val) + const buf = ref.alloc(ref.types.Object) as Pointer + ref.writeObject(buf, 0, obj) + return buf }) const lstr = ffi.Callback(PgfLiteral, [PgfUnmarshaller, PgfTextPtr], function (self, val: Pointer): Pointer { - return 0 as any + const jsval = PgfText_AsString(val) + const obj = new Literal(jsval) + const buf = ref.alloc(ref.types.Object) as Pointer + ref.writeObject(buf, 0, obj) + return buf }) const dtyp = ffi.Callback(PgfType, [PgfUnmarshaller, ref.types.int, PgfTypeHypoPtr, PgfTextPtr, ref.types.int, ref.refType(PgfExpr)], @@ -249,8 +256,8 @@ const dtyp = ffi.Callback(PgfType, [PgfUnmarshaller, ref.types.int, PgfTypeHypoP return 0 as any }) -const free_ref = ffi.Callback(ref.types.void, [PgfUnmarshaller, ref.refType(ref.types.void)], - function (self, x: any): void { +const free_ref = ffi.Callback(ref.types.void, [PgfUnmarshaller, ref.refType(ref.types.Object)], + function (self, x: Pointer): void { }) const un_vtbl = new PgfUnmarshallerVtbl({ diff --git a/src/runtime/javascript/tests/basic.test.ts b/src/runtime/javascript/tests/basic.test.ts index 51c6433ba..3146146c3 100644 --- a/src/runtime/javascript/tests/basic.test.ts +++ b/src/runtime/javascript/tests/basic.test.ts @@ -209,7 +209,7 @@ describe('abstract syntax', () => { // ---------------------------------------------------------------------------- -describe('expressions', () => { +describe.only('expressions', () => { test('small integer', () => { const e1 = PGF.readExpr('123') const e2 = new PGF.ExprLit(123) @@ -253,4 +253,36 @@ describe('expressions', () => { expect(e1).toEqual(e2) expect(e1).not.toEqual(e3) }) + + test('float', () => { + const e1 = PGF.readExpr('3.142') + const e2 = new PGF.ExprLit(3.142) + const e3 = new PGF.ExprLit(2.014) + expect(e1).toEqual(e2) + expect(e1).not.toEqual(e3) + }) + + test('negative float', () => { + const e1 = PGF.readExpr('-3.142') + const e2 = new PGF.ExprLit(-3.142) + const e3 = new PGF.ExprLit(-2.014) + expect(e1).toEqual(e2) + expect(e1).not.toEqual(e3) + }) + + test('string', () => { + const e1 = PGF.readExpr('"abc"') + const e2 = new PGF.ExprLit('abc') + const e3 = new PGF.ExprLit('def') + expect(e1).toEqual(e2) + expect(e1).not.toEqual(e3) + }) + + test('string unicode', () => { + const e1 = PGF.readExpr('"açġħ"') + const e2 = new PGF.ExprLit('açġħ') + const e3 = new PGF.ExprLit('acgh') + expect(e1).toEqual(e2) + expect(e1).not.toEqual(e3) + }) })