0.8.18.36:
[sbcl.git] / tests / foreign.test.sh
index aac8a70..bd91bfe 100644 (file)
@@ -25,21 +25,31 @@ testfilestem=${TMPDIR:-/tmp}/sbcl-foreign-test-$$
 
 ## Make a little shared object files to test with.
 
+build_so() {
+  echo building $1.so
+  if [ $(uname -p) = x86_64 ]; then
+    CFLAGS="$CFLAGS -fPIC"
+  fi
+  cc -c $1.c -o $1.o $CFLAGS
+  ld -shared -o $1.so $1.o
+}
+    
 echo 'int summish(int x, int y) { return 1 + x + y; }' > $testfilestem.c
 echo 'int numberish = 42;' >> $testfilestem.c
 echo 'int nummish(int x) { return numberish + x; }' >> $testfilestem.c
-cc -c $testfilestem.c -o $testfilestem.o
-ld -shared -o $testfilestem.so $testfilestem.o
+build_so $testfilestem
 
-echo 'int foo = 13;' > $testfilestem-foobar.c
-echo 'int bar() { return 42; }' >> $testfilestem-foobar.c
-cc -c $testfilestem-foobar.c -o $testfilestem-foobar.o
-ld -shared -o $testfilestem-foobar.so $testfilestem-foobar.o
+echo 'int foo = 13;' > $testfilestem-b.c
+echo 'int bar() { return 42; }' >> $testfilestem-b.c
+build_so $testfilestem-b
 
-echo 'int foo = 42;' > $testfilestem-foobar2.c
-echo 'int bar() { return 13; }' >> $testfilestem-foobar2.c
-cc -c $testfilestem-foobar2.c -o $testfilestem-foobar2.o
-ld -shared -o $testfilestem-foobar2.so $testfilestem-foobar2.o
+echo 'int foo = 42;' > $testfilestem-b2.c
+echo 'int bar() { return 13; }' >> $testfilestem-b2.c
+build_so $testfilestem-b2
+
+echo 'int late_foo = 43;' > $testfilestem-c.c
+echo 'int late_bar() { return 14; }' >> $testfilestem-c.c
+build_so $testfilestem-c
 
 ## Foreign definitions & load
 
@@ -50,7 +60,7 @@ cat > $testfilestem.def.lisp <<EOF
     (handler-case
         (progn
           (load-shared-object "$testfilestem.so")
-          (load-shared-object "$testfilestem-foobar.so"))
+          (load-shared-object "$testfilestem-b.so"))
       (sb-int:unsupported-operator ()
         ;; At least as of sbcl-0.7.0.5, LOAD-SHARED-OBJECT isn't
         ;; supported on every OS. In that case, there's nothing to test,
@@ -70,6 +80,16 @@ cat > $testfilestem.def.lisp <<EOF
   ;; that the location will be the same.
   (assert (= (sb-sys:sap-int (alien-sap *environ*))
              (sb-sys:sap-int (alien-sap environ))))
+
+  ;; automagic restarts
+  (setf *debugger-hook* 
+        (lambda (condition hook)
+          (print (list :debugger-hook condition))
+          (let ((cont (find-restart 'continue condition)))
+            (when cont 
+              (invoke-restart cont)))
+          (print :fell-through)
+          (invoke-debugger condition)))
 EOF
 
 # Test code
@@ -80,16 +100,35 @@ cat > $testfilestem.test.lisp <<EOF
   (assert (= 13 numberish))
   (assert (= 14 (nummish 1)))
 
+  (print :stage-1)
+
+  ;; test realoading object file with new definitions
   (assert (= 13 foo))
   (assert (= 42 (bar)))
-  ;; test realoading object file with new definitions
-  (rename-file "$testfilestem-foobar.so" "$testfilestem-foobar.bak")
-  (rename-file "$testfilestem-foobar2.so" "$testfilestem-foobar.so")
-  (load-shared-object "$testfilestem-foobar.so")
+  (rename-file "$testfilestem-b.so" "$testfilestem-b.bak")
+  (rename-file "$testfilestem-b2.so" "$testfilestem-b.so")
+  (load-shared-object "$testfilestem-b.so")
   (assert (= 42 foo))
   (assert (= 13 (bar)))
-  (rename-file "$testfilestem-foobar.so" "$testfilestem-foobar2.so")
-  (rename-file "$testfilestem-foobar.bak" "$testfilestem-foobar.so")
+  (rename-file "$testfilestem-b.so" "$testfilestem-b2.so")
+  (rename-file "$testfilestem-b.bak" "$testfilestem-b.so")
+
+  (print :stage-2)
+
+  ;; test late resolution
+  (define-alien-variable late-foo int)
+  (define-alien-routine late-bar int)
+  (multiple-value-bind (val err) (ignore-errors late-foo)
+    (assert (not val))
+    (assert (typep err 'undefined-alien-error)))
+  (multiple-value-bind (val err) (ignore-errors (late-bar))
+    (assert (not val))
+    (assert (typep err 'undefined-alien-error)))
+  (load-shared-object "$testfilestem-c.so")
+  (assert (= 43 late-foo))
+  (assert (= 14 (late-bar)))
+
+  (print :stage-3)
 
   (sb-ext:quit :unix-status 52) ; success convention for Lisp program
 EOF
@@ -103,6 +142,8 @@ else
     exit 1
 fi
 
+echo compile ok
+
 ${SBCL:-sbcl} --load $testfilestem.def.fasl --load $testfilestem.test.lisp
 RET=$?
 if [ $RET = 22 ]; then
@@ -114,6 +155,8 @@ elif [ $RET != 52 ]; then
     exit 1 
 fi
 
+echo load ok
+
 ${SBCL:-sbcl} --load $testfilestem.def.fasl --eval "(when (member :linkage-table *features*) (save-lisp-and-die \"$testfilestem.core\"))" <<EOF
   (sb-ext:quit :unix-status 22) ; catch this
 EOF
@@ -122,6 +165,8 @@ if [ $? = 22 ]; then
     exit $PUNT # success -- linkage-table not available
 fi
 
+echo table ok
+
 $SBCL_ALLOWING_CORE --core $testfilestem.core --sysinit /dev/null --userinit /dev/null --load $testfilestem.test.lisp
 if [ $? != 52 ]; then
     rm $testfilestem.*
@@ -129,6 +174,28 @@ if [ $? != 52 ]; then
     exit 1 # Failure
 fi
 
+echo start ok
+
+# missing object file
+rm $testfilestem-b.so $testfilestem-b2.so
+$SBCL_ALLOWING_CORE --core $testfilestem.core --sysinit /dev/null --userinit /dev/null <<EOF
+  (assert (= 22 (summish 10 11)))
+  (multiple-value-bind (val err) (ignore-errors (eval 'foo))
+    (assert (not val))
+    (assert (typep err 'undefined-alien-error)))
+  (multiple-value-bind (val err) (ignore-errors (eval '(bar)))
+    (assert (not val))
+    (assert (typep err 'undefined-alien-error)))
+  (quit :unix-status 52)
+EOF
+if [ $? != 52 ]; then
+    rm $testfilestem.*
+    echo test failed: $?
+    exit 1 # Failure
+fi
+
+echo missing ok
+
 rm $testfilestem.*
 
 # success convention for script