From 40e943027866f4bc126d0a57a15014a50644f665 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Fri, 17 Jun 2022 17:24:52 -0500 Subject: [PATCH] Move examples to example directory. --- CHANGELOG.md | 4 +- Makefile | 5 +- .../gtknew.janet => examples/ffi/gtk.janet | 0 {ffitest => examples/ffi}/so.c | 0 {ffitest => examples/ffi}/test.janet | 0 ffitest/gtk.janet | 57 ------------------- 6 files changed, 4 insertions(+), 62 deletions(-) rename ffitest/gtknew.janet => examples/ffi/gtk.janet (100%) rename {ffitest => examples/ffi}/so.c (100%) rename {ffitest => examples/ffi}/test.janet (100%) delete mode 100644 ffitest/gtk.janet diff --git a/CHANGELOG.md b/CHANGELOG.md index 36476676..d7742bd1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,10 +2,12 @@ All notable changes to this project will be documented in this file. ## Unreleased - ??? -- Add `ffi/` module for interfacing with dynamic libraries and raw function pointers. +- Add experimental `ffi/` module for interfacing with dynamic libraries and raw function pointers. - Allow using `&named` in function prototypes for named arguments. This is a more ergonomic variant of `&keys` that isn't as redundant, more self documenting, and allows extension to things like default arguments. +- Add `delay` macro for lazy evaluate-and-save thunks. +- Remove pthread.h from janet.h for easier includes. - Add `debugger` - an easy to use debugger function that just takes a fiber. - `dofile` will now start a debugger on errors if the environment it is passed has `:debug` set. - Add `debugger-on-status` function, which can be passed to `run-context` to start a debugger on diff --git a/Makefile b/Makefile index ad562ee7..dd8e0f25 100644 --- a/Makefile +++ b/Makefile @@ -227,9 +227,6 @@ valtest: $(JANET_TARGET) $(TEST_PROGRAMS) callgrind: $(JANET_TARGET) for f in test/suite*.janet; do valgrind --tool=callgrind ./$(JANET_TARGET) "$$f" || exit; done -ffitest: $(JANET_TARGET) - $(JANET_TARGET) ffitest/test.janet - ######################## ##### Distribution ##### ######################## @@ -370,5 +367,5 @@ help: @echo ' make grammar Generate a TextMate language grammar' @echo -.PHONY: clean install repl debug valgrind test ffitest \ +.PHONY: clean install repl debug valgrind test \ valtest dist uninstall docs grammar format help compile-commands diff --git a/ffitest/gtknew.janet b/examples/ffi/gtk.janet similarity index 100% rename from ffitest/gtknew.janet rename to examples/ffi/gtk.janet diff --git a/ffitest/so.c b/examples/ffi/so.c similarity index 100% rename from ffitest/so.c rename to examples/ffi/so.c diff --git a/ffitest/test.janet b/examples/ffi/test.janet similarity index 100% rename from ffitest/test.janet rename to examples/ffi/test.janet diff --git a/ffitest/gtk.janet b/ffitest/gtk.janet deleted file mode 100644 index a2e86d53..00000000 --- a/ffitest/gtk.janet +++ /dev/null @@ -1,57 +0,0 @@ -# FFI is best used with a wrapper like the one below -# An even more sophisticated macro wrapper could add -# better doc strings, better parameter checking, etc. - -(defn ffi-context - "Load a dynamic library and set it as the context for following declarations" - [location] - (setdyn :ffi-context (ffi/native location))) - -(defmacro defnative - "Declare a native binding" - [name ret-type & body] - (def signature-args (last body)) - (def defn-args (seq [_ :in signature-args] (gensym))) - (def raw-symbol (string/replace-all "-" "_" name)) - (def $sig (symbol name "-signature-")) - (def $pointer (symbol name "-raw-pointer-")) - ~(upscope - (def ,$pointer :private (as-macro ,assert (,ffi/lookup (,dyn :ffi-context) ,raw-symbol))) - (def ,$sig :private (,ffi/signature :default ,ret-type ,;signature-args)) - (defn ,name [,;defn-args] - (,ffi/call ,$pointer ,$sig ,;defn-args)))) - -(ffi-context "/usr/lib/libgtk-3.so") - -(defnative gtk-application-new :ptr [:ptr :uint]) -(defnative g-signal-connect-data :ulong [:ptr :ptr :ptr :ptr :ptr :int]) -(defnative g-application-run :int [:ptr :int :ptr]) -(defnative gtk-application-window-new :ptr [:ptr]) -(defnative gtk-button-new-with-label :ptr [:ptr]) -(defnative gtk-container-add :void [:ptr :ptr]) -(defnative gtk-widget-show-all :void [:ptr]) -(defnative gtk-button-set-label :void [:ptr :ptr]) - -# GTK follows a strict convention for callbacks. This lets us use -# a single "standard" callback whose behavior is specified by userdata. -# This lets use callbacks without code generation, so no issues with iOS, SELinux, etc. -# Limitation is that we cannot generate arbitrary closures to pass into apis. -# However, any stubs we need we would simply need to compile ourselves, so -# Janet includes a common stub out of the box. -(def cb (ffi/trampoline :default)) - -(defn on-active - [app] - (def window (gtk-application-window-new app)) - (def btn (gtk-button-new-with-label "Click Me!")) - (g-signal-connect-data btn "clicked" cb - (fn [btn] (gtk-button-set-label btn "Hello World")) - nil 1) - (gtk-container-add window btn) - (gtk-widget-show-all window)) - -(defn main - [&] - (def app (gtk-application-new "org.janet-lang.example.HelloApp" 0)) - (g-signal-connect-data app "activate" cb on-active nil 1) - (g-application-run app 0 nil))