From e0b09defd048208f3a88f7d078a797c0b70b1b04 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexis=20Laferri=C3=A8re?= Date: Thu, 5 Feb 2015 10:11:54 -0500 Subject: [PATCH] tests: test calling extern constructors from extern code MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexis Laferrière --- tests/sav/test_ffi_c_new_extern.res | 2 ++ tests/test_ffi_c_new_extern.nit | 45 +++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 tests/sav/test_ffi_c_new_extern.res create mode 100644 tests/test_ffi_c_new_extern.nit diff --git a/tests/sav/test_ffi_c_new_extern.res b/tests/sav/test_ffi_c_new_extern.res new file mode 100644 index 0000000..ae8c4c1 --- /dev/null +++ b/tests/sav/test_ffi_c_new_extern.res @@ -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 index 0000000..e9b46d2 --- /dev/null +++ b/tests/test_ffi_c_new_extern.nit @@ -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 -- 1.7.9.5