Commits

Kota UENISHI committed cec8561

OCaml C interface, doesn't work well

Comments (0)

Files changed (4)

 # The standard OMakefile.
 # You will usually need to modify this file for your project.
 
-# Delete this line once you have configured this file
-# eprintln($(CWD)/OMakefile is not configured)
+OCAML_WHERE=$(shell ocamlc -where)
 
 ########################################################################
 # Phony targets are scoped, so you probably want to declare them first.
 # configuration variables.
 #
 
-# CFLAGS +=
+CFLAGS += -g
 # ASFLAGS +=
 # LDFLAGS +=
-# INCLUDES +=
+INCLUDES += $(OCAML_WHERE)
 
 ################################################
 # Uncomment the following section if you want
 # in the current directory.
 #
 
-# LIBFILES[] =
+LIBFILES[] =
+    chello
 #    file1
 #    file2
 #
-# LIB = libxxx
-#
-# .DEFAULT: $(StaticCLibrary $(LIB), $(LIBFILES))
+LIB = libhello
+
+.DEFAULT: $(StaticCLibrary $(LIB), $(LIBFILES))
 
 ########################################################################
 # OCaml configuration.
 .DEFAULT: $(OCamlProgram mysh, mysh)
 OCAML_OTHER_LIBS += str
 .DEFAULT: $(OCamlProgram grep, grep)
-.DEFAULT: $(OCamlProgram hello, hello)
 .DEFAULT: $(OCamlProgram prime, prime)
 .DEFAULT: $(OCamlProgram qsort, qsort)
+
+OCAML_CLIBS += libhello
+.DEFAULT: $(OCamlProgram hello, hello)
+#include <caml/mlvalues.h>
+#include <caml/memory.h>
+#include <caml/threads.h>
+
+#define Nothing ((value) 0)
+
+#include <caml/alloc.h>
+#include <caml/fail.h>
+
+#include <stdio.h>
+
+#ifndef __APPLE__
+#error "use MacOSX. *BSD and Linux is in progress"
+#endif
+//__GNUC__ 4
+//__amd64 1
+//__x86_64 1
+//__x86_64__ 1
+
+#define PIN \
+  printf("%s:%d entered into %s\n", __FILE__, __LINE__, __func__ );
+#define POUT \
+  printf("%s:%d get out from %s\n", __FILE__, __LINE__, __func__ );
+#define HERE(x) \
+  printf("%s:%d in %s -> %p\n", __FILE__, __LINE__, __func__, x );
+
+CAMLprim value hello(value arg){
+  CAMLparam1(arg);
+  printf("hello %s!\n", String_val(arg));
+  CAMLreturn(Val_unit);
+}
+
+CAMLprim value env(void){
+  CAMLparam0();
+  CAMLreturn(caml_copy_string(__VERSION__));
+}
+
+CAMLprim value hoge(void){
+  CAMLparam0();
+  CAMLlocal1(v_res);
+  int i=10;
+  value v_ev;
+  PIN;
+  //  printf("len=%d : %p\n", i, v_res);
+  v_res = caml_alloc(i, 0);
+  while (--i >= 0) {
+    v_ev = caml_alloc_small(2,0); 
+    Field(v_ev, 0) = i;
+    Field(v_ev, 1) = caml_copy_int32( 23 );
+    printf("len=%d : %p\n", i, v_ev);
+    Store_field(v_res, i, v_ev);
+  }
+  POUT;
+  CAMLreturn(v_res);
+}
+
-Pervasives.print_string "Hello World!\n";;
 
+external hello : string -> unit = "hello"
+external env : unit -> string = "env"
+
+external hoge : unit -> (int * int) list = "hoge"
+
+let _ =
+  hello "hoge";
+  List.iter (fun (i,j) -> Printf.printf "(%d,%d)\n" i j) ((3,4)::(hoge()));
+  Pervasives.print_string "Hello World!\n";;
+  
+val hello : string -> unit
+val env : unit -> string
+val hoge : unit -> (int * int) list