summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
authorMichael Jendrusch <Jendrusch@stud.uni-heidelberg.de>2017-01-17 16:43:06 +0100
committerAndreas Rumpf <rumpf_a@web.de>2017-01-17 16:43:06 +0100
commit454547da8efe7e07b0c13254c6a000e805e2475e (patch)
tree08f4349d2c51670dd3b8b7d6d30cc4b4c244ad18 /tests
parent54a6c5b34844322f2bb50222cb779dde02c6cad4 (diff)
downloadNim-454547da8efe7e07b0c13254c6a000e805e2475e.tar.gz
Removed mangling of object fields for the js target only. (#5226)
* removed mangling of object fields for the js target only.

* changed default mangling behaviour for the php target as well.

* Added test for unorthodox field names (reserved words and operators). Adjusted field accessors and object constructors / new to be ECMAScript first edition compatible, when using fieldnames which are reserved words.
Diffstat (limited to 'tests')
-rw-r--r--tests/js/tmangle.nim83
1 files changed, 83 insertions, 0 deletions
diff --git a/tests/js/tmangle.nim b/tests/js/tmangle.nim
new file mode 100644
index 000000000..91193d5a7
--- /dev/null
+++ b/tests/js/tmangle.nim
@@ -0,0 +1,83 @@
+discard """
+  output: '''true
+true
+true
+true
+true'''
+"""
+
+# Test not mangled:
+block:
+  type T = object
+    a: int
+    b: cstring
+  proc test(): bool =
+    let obj = T(a: 11, b: "foo")
+    {. emit: [result, " = (", obj, ".a == 11);"] .}
+    {. emit: [result, " = ", result, " && (", obj, ".b == \"foo\");"] .}
+  echo test()
+
+# Test indirect (fields in genAddr):
+block:
+  type T = object
+    a: int
+    b: cstring
+  var global = T(a: 11, b: "foo")
+  proc test(): bool =
+    var obj = T(a: 11, b: "foo")
+    {. emit: [result, " = (", obj.addr[], "[0].a == 11);"] .}
+    {. emit: [result, " = ", result, " && (", obj.addr[], "[0].b == \"foo\");"] .}
+    {. emit: [result, " = ", result, " && (", global, "[0].a == 11);"] .}
+    {. emit: [result, " = ", result, " && (", global, "[0].b == \"foo\");"] .}
+  echo test()
+
+# Test addr of field:
+block:
+  type T = object
+    a: int
+    b: cstring
+  proc test(): bool =
+    var obj = T(a: 11, b: "foo")
+    result = obj.a.addr[] == 11
+    result = result and obj.b.addr[] == "foo".cstring
+  echo test()
+
+# Test reserved words:
+block:
+  type T = ref object
+    `if`: int
+    `for`: int
+    `==`: cstring
+    `&&`: cstring
+  proc test(): bool =
+    var
+      obj1 = T(`if`: 11, `for`: 22, `==`: "foo", `&&`: "bar")
+      obj2: T
+    new obj2 # Test behaviour for createRecordVarAux.
+    result = obj1.`if` == 11
+    result = result and obj1.addr[].`for` == 22
+    result = result and obj1.`==` == "foo".cstring
+    result = result and obj1.`&&`.addr[] == "bar".cstring
+    result = result and obj2.`if` == 0
+    result = result and obj2.`for` == 0
+    result = result and obj2.`==`.isNil()
+    result = result and obj2.`&&`.isNil()
+  echo test()
+
+# Test importc / exportc fields:
+block:
+  type T = object
+    a: int
+    b {. importc: "notB" .}: cstring
+  type U = object
+    a: int
+    b {. exportc: "notB" .}: cstring
+  proc test(): bool =
+    var
+      obj1 = T(a: 11, b: "foo")
+      obj2 = U(a: 11, b: "foo")
+    {. emit: [result, " = (", obj1, ".a == 11);"] .}
+    {. emit: [result, " = ", result, " && (", obj1, ".notB == \"foo\");"] .}
+    {. emit: [result, " = (", obj2, ".a == 11);"] .}
+    {. emit: [result, " = ", result, " && (", obj2, ".notB == \"foo\");"] .}
+  echo test()