Skip to content

Commit

Permalink
Constructor inheritance, also app.nim now contains an actually sort o…
Browse files Browse the repository at this point in the history
…f sensible test
  • Loading branch information
mcclure committed Aug 9, 2016
1 parent 2aee9d2 commit 1efb636
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 36 deletions.
42 changes: 32 additions & 10 deletions src/app.nim
Original file line number Diff line number Diff line change
@@ -1,18 +1,40 @@
import tsGlue

let two* {.exportc.} = QTALKTOME
let twoString* {.exportc.} = QTALKTOME2
# POST

let three* {.exportc.} = QADDONE(QTALKTOME)
var failed = false
try:
testAssert(false, "Expecting to fail")
except:
failed = true
testAssert(failed, "Self-test of test script")

QCONSOLELOG("two plus one is " & $three)
# Actual tests

QDEKZ.ONE = 3
QCONSOLELOG("three plus two is " & $QDEKZ.FUNC(QDEKZ.ONE))
let QBbackflow* {.exportc.} = 543

let four : QDEK1 = newQDEK1()
let five : QDEK2 = newQDEK2("Concat ")
testAssert(QBnumber == 2, "Number variable")
testAssert(QBstring == "OK", "String variable")

QCONSOLELOG("two plus three is " & $four.FUNC(3))
testAssert(QFaddone(5) == 6, "Call function")

QCONSOLELOG(five.FUNC2("working"))
testAssert(QIchild.num3 == 3, "Check instance")

QIchild.num3 = 11
testAssert(QIchild.num3 == 11, "Write instance")

QIChild.addNum(5)
testAssert(QIchild.num3 == 16, "Read instance 2")

let inst1 : QCbase = newQCbase()
let inst2 : QCbase = newQCchild("glow")
let inst3 : QCgrand = newQCgrand("black")
let inst4 = newQCorder()

testAssert(inst1.add2(17) == 19, "Call method")
testAssert(inst2.add2(19) == 21, "Call method 2")
testAssert(inst3.addStr("light") == "blacklight", "Inherit constructor")
testAssert(inst4.grand.addStr("fish") == "silverfish", "Inherit constructor 2")

# TODO: Needs function-typed variables
# testAssert(QFbackflow() == 543, "Exportc")
62 changes: 38 additions & 24 deletions src/tsGlue.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,66 @@
function testAssert(condition: boolean, message:string) {
if (!condition)
throw new Error("Test failed: " + message)
}

let QTALKTOME = 2
let QTALKTOME2 = "OK"
let QBnumber = 2
let QBstring = "OK"

function QADDONE(x:number) {
function QFaddone(x:number) {
return x + 1
}

class QDEK1 {
ONE:number
TWO:string
class QCorder {
grand: QCgrand
constructor() {
this.grand = new QCgrand("silver")
}
}

class QCbase {
num1:number
str2:string

FUNC(x:number) { return x + 2 }
add2(x:number) { return x + 2 }
}

class QDEK2 extends QDEK1 {
THREE:number
FOUR:string
class QCchild extends QCbase {
num3:number
str4:string
constructor(ok:string) {
super()
this.THREE = 3
this.FOUR = ok
this.num3 = 3
this.str4 = ok
}
FUNC2(y:string) { return this.FOUR + y }
addNum(y:number) { this.num3 += y }
}

let QDEKZ = new QDEK2("3")

function QCONSOLELOG(s:string) {
console.log(s)
class QCgrand extends QCchild {
addStr(y:string) { return this.str4 + y }
}

let XGY : QDEK1[]
let QIchild = new QCchild("3")

let QAarray : QCbase[]

class XQ1<T> {
class QCgeneric<T> {
x: T
}

let XQ2 : XQ1<number>
let QIgeneric : QCgeneric<number>

class XREC1 {
a1: XREC2
class QCrecursive1 {
a1: QCrecursive2
}

class XREC2 {
constructor(public a2 : XREC1) {
class QCrecursive2 {
constructor(public a2 : QCrecursive1) {
a2.a1 = this
}
}

// TODO: Right now we erase mutually exclusive types, but pass through variables typed as one of the erased types
// let XR = new XREC2(new XREC1())

// Ugly way to access global variable
let QFbackflow = () : number => (<any>this).QBbackflow
11 changes: 9 additions & 2 deletions tools/dts2nim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,8 +478,15 @@ class GenVendor {

// Get constructor
// FIXME: Produces garbage on inherited constructors
if (!foundConstructors)
constructors.push(new ConstructorGen([]))
if (!foundConstructors) {
if (inherit && inherit.constructors) {
for (let constructor of inherit.constructors) {
constructors.push(new ConstructorGen( constructor.params ))
}
} else {
constructors.push(new ConstructorGen([]))
}
}

result.init(inherit, fields, constructors, methods)
return result
Expand Down

0 comments on commit 1efb636

Please sign in to comment.