mirror of
https://github.com/janet-lang/janet
synced 2024-11-28 02:59:54 +00:00
Move examples to example directory.
This commit is contained in:
parent
62fc55fc74
commit
40e9430278
@ -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
|
||||
|
5
Makefile
5
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
|
||||
|
@ -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))
|
Loading…
Reference in New Issue
Block a user