tests: test calling extern constructors from extern code
authorAlexis Laferrière <alexis.laf@xymus.net>
Thu, 5 Feb 2015 15:11:54 +0000 (10:11 -0500)
committerAlexis Laferrière <alexis.laf@xymus.net>
Thu, 5 Feb 2015 17:15:01 +0000 (12:15 -0500)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

tests/sav/test_ffi_c_new_extern.res [new file with mode: 0644]
tests/test_ffi_c_new_extern.nit [new file with mode: 0644]

diff --git a/tests/sav/test_ffi_c_new_extern.res b/tests/sav/test_ffi_c_new_extern.res
new file mode 100644 (file)
index 0000000..ae8c4c1
--- /dev/null
@@ -0,0 +1,2 @@
+1234
+5678
diff --git a/tests/test_ffi_c_new_extern.nit b/tests/test_ffi_c_new_extern.nit
new file mode 100644 (file)
index 0000000..e9b46d2
--- /dev/null
@@ -0,0 +1,45 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Test callback to an extern constructor from extern code
+
+import standard::string
+
+extern class IntPtr `{ int* `}
+       new (v: Int) `{
+               int *r = malloc(sizeof(int));
+               *r = v;
+               return r;
+       `}
+
+       redef fun to_s import NativeString, NativeString.to_s `{
+               int len = snprintf(NULL, 0, "%d", *recv) + 1;
+               char *c = new_NativeString(len);
+               sprintf(c, "%d", *recv);
+               return NativeString_to_s(c);
+       `}
+end
+
+fun foo: IntPtr import IntPtr `{
+       int *c = new_IntPtr(5678);
+       return c;
+`}
+
+var a = new IntPtr(1234)
+a.to_s.output
+"\n".output
+
+var b = foo
+b.to_s.output
+"\n".output