Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update to last version of ctypes #2

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions ISSUE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
###lineoise.mli
I added a lineoise.mli file
with explicit type signatures for all foreign functions.

Without this mli file, example.ml compiles fine *within* the source directory along all stubs and generated files.
But once the package is installed and example.ml compiled in its own directory,
compilation leads to errors like:

File "example.ml", line 7, characters 6-30:
Error: This expression has type
(Linenoise.completions Ctypes.structure Ctypes_static.ptr ->
string -> unit)
Linenoise_foreign.fn
This is not a function; it cannot be applied.

Indeed, all the foreign functions are exported as `(a -> b) Linenoise_foreign.fn`.
The lineoise.mli file fixes the issue with explicit `(a -> b)` signatures.

This works.
But I fail to understand why this file is required (this doesn't seems the case in other ctypes packages),
and how this works (why the compiler accepts the foreign functions within the project but not once installed).
What am I missing ?
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ ocaml setup.ml -install
```
## Rebuilding the Stubs
```bash
ocamlfind ocamlc -linkpkg -g -package ctypes -package ctypes.foreign -package ctypes.stubs linenoise_bindings.ml -o linenoise_bindings.native
./linenoise_bindings.native
ocamlbuild -use-ocamlfind -pkgs ctypes,ctypes.foreign,ctypes.stubs generate.native
./generate.native
```

## Building the example
The example is ported from the C example. It is functionally identical

```bash
ocamlfind ocamlopt -linkpkg -g -thread -package core -package ctypes -package ctypes.foreign -package linenoise example.ml -o example.native
ocamlfind ocamlopt -linkpkg -g -thread -package core -package linenoise example.ml -o example.native
```
26 changes: 21 additions & 5 deletions _oasis
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,31 @@ Authors: Katherine Whitlock
License: BSD-3-clause
Plugins: META(0.4)

Executable generate
Path: .
MainIs: generate.ml
BuildDepends: ctypes.foreign, ctypes.stubs
Install: false
BuildTools: ocamlbuild

Library linenoise
Path: .
Modules: Linenoise
InternalModules: Linenoise_bindings, Linenoise_foreign
CSources: linenoise_generated.c, linenoise_c/linenoise.c, linenoise_c/linenoise.h
CCOpt: -I $pkg_ctypes_stubs
BuildDepends: ctypes.foreign, ctypes.stubs
BuildTools: ocamlbuild
Install: true

Executable example
Path: .
BuildTools: ocamlbuild
CSources: linenoise_stubs.c, linenoise_c/linenoise.c, linenoise_c/linenoise.h, headers/complex_stubs.h, headers/cstubs_internals.h, headers/managed_buffer_stubs.h,headers/primitives.h, headers/raw_pointer.h, headers/type_info_stubs.h, headers/unsigned_stubs.h
BuildDepends: ctypes.foreign, ctypes.stubs, core, threads
Modules: Linenoise_bindings, Linenoise
Install: true
MainIs: example.ml
BuildDepends: linenoise, core, threads
CompiledObject: native
Install: false


SourceRepository head
Type: git
Location: https://github.com/brick-lang/linenoise-ml
Expand Down
41 changes: 22 additions & 19 deletions example.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
open Core.Std
open Linenoise
open Core.Std

let completion (buf:string) lc =
if (String.get buf 0) = 'h' then
Expand All @@ -17,35 +17,38 @@ let () =
if (Array.get Sys.argv !count) = "--multiline" then
begin
Linenoise.set_multi_line 1;
print_string "Multi-line mode enabled"
Printf.printf "Multi-line mode enabled\n%!"
end
else
begin
Printf.fprintf stderr "Usage: %s [--multiline]\n" prgname;
Printf.fprintf stderr "Usage: %s [--multiline]\n%!" prgname;
exit 1
end
done;

Linenoise.set_completion_callback completion;
ignore (Linenoise.history_load "history.txt");

let line = ref (Linenoise.linenoise "hello> ") in
while !line <> "" do

if (String.get !line 0) <> '/' then
let rec loop () =
match Linenoise.linenoise "hello> " with
| None -> ()
| Some "" -> loop ()
| Some line when String.get line 0 <> '/' ->
begin
Printf.printf "echo: '%s'\n" !line;
ignore (Linenoise.history_add !line);
Printf.printf "echo: '%s'\n%!" line;
ignore (Linenoise.history_add line);
ignore (Linenoise.history_save "history.txt");
loop ()
end
else if (String.slice !line 0 11) = "/historylen" then
begin
let len = Int.of_string (String.slice !line 11 0) in
ignore (Linenoise.history_set_max_len len)
| Some line ->
begin match String.split ~on:' ' line with
| ["/historylen"; arg] ->
let len = Int.of_string (String.strip arg) in
ignore (Linenoise.history_set_max_len len);
loop ()
| _ ->
Printf.printf "Unrecognized command: %s\n%!" line;
loop ()
end
else if (String.get !line 0) = '/' then
Printf.printf "Unrecognized command: %s\n" !line
else assert false;
line := (Linenoise.linenoise "hello> ")
done;
exit 0
in
loop ()
21 changes: 21 additions & 0 deletions generate.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
open Ctypes

let c_headers = "
#include <stdlib.h>
#include \"linenoise_c/linenoise.h\"
"

let main () =
let ml_out = open_out "linenoise_foreign.ml"
and c_out = open_out "linenoise_generated.c" in
let ml_fmt = Format.formatter_of_out_channel ml_out
and c_fmt = Format.formatter_of_out_channel c_out in
Format.fprintf c_fmt "%s@\n" c_headers;
Cstubs.write_c c_fmt ~prefix:"linenoise_stub" (module Linenoise_bindings.Make);
Cstubs.write_ml ml_fmt ~prefix:"linenoise_stub" (module Linenoise_bindings.Make);
Format.pp_print_flush ml_fmt ();
Format.pp_print_flush c_fmt ();
close_out ml_out;
close_out c_out

let () = main ()
26 changes: 0 additions & 26 deletions headers/complex_stubs.h

This file was deleted.

6 changes: 0 additions & 6 deletions headers/cstubs_internals.h

This file was deleted.

22 changes: 0 additions & 22 deletions headers/managed_buffer_stubs.h

This file was deleted.

112 changes: 0 additions & 112 deletions headers/primitives.h

This file was deleted.

27 changes: 0 additions & 27 deletions headers/raw_pointer.h

This file was deleted.

21 changes: 0 additions & 21 deletions headers/type_info_stubs.h

This file was deleted.

Loading