Created
January 6, 2022 18:17
-
-
Save drewc/e615bc940d1bde138c3b25f88b614ada to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(declare (block) (standard-bindings) (extended-bindings)) | |
(begin | |
(define drewc/gurf/surf#surf-driver! | |
(lambda () | |
(letrec* ((_min-sleep119_ '1e-5) | |
(_max-sleep120_ '.05) | |
(_sleep-incr121_ '1e-8) | |
(_sleep122_ _min-sleep119_) | |
(_sleepy123_ | |
(lambda () | |
(thread-sleep! (max _sleep122_ _max-sleep120_)) | |
(if (< _sleep122_ _max-sleep120_) | |
(+ _sleep122_ _sleep-incr121_) | |
'#!void)))) | |
(let _lp125_ () | |
(letrec* ((_events?128_ (drewc/gurf/surf#gtk_surf_iteration))) | |
(if _events?128_ (set! _sleep122_ _min-sleep119_) (_sleepy123_)) | |
(_lp125_)))))) | |
(define drewc/gurf/surf#current-surf-driver (make-parameter '#f)) | |
(define drewc/gurf/surf#start-surfing! | |
(lambda () | |
(let ((_$e113_ (drewc/gurf/surf#current-surf-driver))) | |
(if _$e113_ | |
(values _$e113_) | |
(begin | |
(drewc/gurf/surf#setup) | |
(let ((_drv116_ | |
(gerbil/gambit/threads#spawn | |
drewc/gurf/surf#surf-driver!))) | |
(drewc/gurf/surf#current-surf-driver _drv116_) | |
_drv116_)))))) | |
(define drewc/gurf/surf#surf__% | |
(lambda (_uri92_ _rclient93_) | |
(drewc/gurf/surf#start-surfing!) | |
(let ((_client95_ (drewc/gurf/surf#newclient _rclient93_))) | |
(drewc/gurf/surf#showview _client95_) | |
(drewc/gurf/surf#loaduri _client95_ _uri92_) | |
(drewc/gurf/surf#updatetitle _client95_) | |
(drewc/gurf/surf#current-surf-client _client95_) | |
_client95_))) | |
define drewc/gurf/surf#surf__0 | |
(lambda () | |
(let* ((_uri101_ '"about:blank") | |
(_rclient103_ (drewc/gurf/surf#current-surf-client))) | |
(drewc/gurf/surf#surf__% _uri101_ _rclient103_)))) | |
(define drewc/gurf/surf#surf__1 | |
(lambda (_uri105_) | |
(let ((_rclient107_ (drewc/gurf/surf#current-surf-client))) | |
(drewc/gurf/surf#surf__% _uri105_ _rclient107_)))) | |
(define drewc/gurf/surf#surf | |
(lambda _g3730_ | |
(let ((_g3729_ (let () (declare (not safe)) (##length _g3730_)))) | |
(cond ((let () (declare (not safe)) (##fx= _g3729_ 0)) | |
(apply drewc/gurf/surf#surf__0 _g3730_)) | |
((let () (declare (not safe)) (##fx= _g3729_ 1)) | |
(apply drewc/gurf/surf#surf__1 _g3730_)) | |
((let () (declare (not safe)) (##fx= _g3729_ 2)) | |
(apply drewc/gurf/surf#surf__% _g3730_)) | |
(else | |
(##raise-wrong-number-of-arguments-exception | |
drewc/gurf/surf#surf | |
_g3730_)))))) | |
(define drewc/gurf/surf#surf-clients | |
(lambda () | |
(let _lp87_ ((_c89_ (drewc/gurf/surf#clients))) | |
(if (not _c89_) | |
'() | |
(cons _c89_ (_lp87_ (drewc/gurf/surf#Client-next _c89_))))))) | |
(define drewc/gurf/surf#current-surf-client (make-parameter '#f)) | |
(define-macro (define-guard guard defn) | |
(if (eval `(cond-expand (,guard #t) (else #f))) | |
'(begin) | |
(begin (eval `(define-cond-expand-feature ,guard)) defn))) | |
(define-macro (define-c-lambda id args ret #!optional (name #f)) | |
(let ((name (or name (symbol->string id)))) | |
`(define ,id (c-lambda ,args ,ret ,name)))) | |
(define-macro (define-const symbol) | |
(let* ((str (symbol->string symbol)) | |
(ref (string-append "___return (" str ");"))) | |
`(define ,symbol ((c-lambda () int ,ref))))) | |
(define-macro (define-const* symbol #!optional (ccond #f)) | |
(let* ((str (symbol->string symbol)) | |
(code (string-append | |
define-macro (define-const* symbol #!optional (ccond #f)) | |
(let* ((str (symbol->string symbol)) | |
(code (string-append | |
"#if " | |
(or ccond (string-append "defined(" str ")")) | |
"\n" | |
"___return (___FIX (" | |
str | |
"));\n" | |
"#else \n" | |
"___return (___FAL);\n" | |
"#endif"))) | |
`(define ,symbol ((c-lambda () scheme-object ,code))))) | |
(define-macro (define-with-errno symbol ffi-symbol args) | |
`(define (,symbol ,@args) | |
(declare (not interrupts-enabled)) | |
(let ((r (,ffi-symbol ,@args))) | |
(if (##fx< r 0) (##fx- (##c-code "___RESULT = ___FIX (errno);")) r)))) | |
(define-macro (define-c-struct | |
struct | |
#!optional | |
(members '()) | |
release-function) | |
(let* ((struct-str (symbol->string struct)) | |
(struct-ptr (string->symbol (string-append struct-str "*"))) | |
(shallow-ptr | |
(string->symbol (string-append struct-str "-shallow-ptr*"))) | |
(borrowed-ptr | |
(string->symbol (string-append struct-str "-borrowed-ptr*"))) | |
(string-types | |
'(char-string | |
nonull-char-string | |
UTF-8-string | |
nonnull-UTF-8-string | |
UTF-16-string | |
nonnull-UTF16-string)) | |
(string-compat-required? | |
(let loop ((m members)) | |
(cond ((null? m) #f) | |
((member (cdr (car m)) string-types) #t) | |
(else (loop (cdr m)))))) | |
(string-setter-body | |
(lambda (member-name) | |
(let ((m (string-append "___arg1->" member-name))) | |
(string-append | |
"if(" | |
m | |
" == NULL)" | |
"\n" | |
m | |
"= strdup(___arg2);" | |
"\n" | |
"else if (strcmp(" | |
m | |
", ___arg2) != 0) {" | |
"\n" | |
"free(" | |
m | |
");" | |
"\n" | |
m | |
"= strdup(___arg2);" | |
"\n" | |
"}" | |
"\n" | |
"___return;" | |
"\n")))) | |
(default-free-body | |
(and string-compat-required? | |
(string-append | |
"___SCMOBJ " | |
struct-str | |
"_ffi_free (void *ptr) {" | |
"\n" | |
"struct " | |
struct-str | |
" *obj = (struct " | |
struct-str | |
"*) ptr;" | |
"\n" | |
(apply string-append | |
(map (lambda (m) | |
(cond ((memq (cdr m) string-types) | |
(let ((mem-name | |
(symbol->string (car m)))) | |
(string-append | |
"if(obj->" | |
mem-name | |
") " | |
"free(obj->" | |
mem-name | |
");" | |
"\n"))) | |
(else ""))) | |
members)) | |
"free(obj);" | |
"\n" | |
"return ___FIX (___NO_ERR);" | |
"\n" | |
"}"))) | |
(release-function | |
(or release-function | |
(if string-compat-required? | |
(string-append struct-str "_ffi_free") | |
"ffi_free"))) | |
(string-compat-types | |
(if string-compat-required? | |
`((c-declare ,default-free-body) | |
(c-define-type | |
,shallow-ptr | |
(pointer ,struct (,struct-ptr) "ffi_free"))) | |
'()))) | |
`(begin | |
(c-define-type ,struct (struct ,struct-str)) | |
(c-define-type | |
,struct-ptr | |
(pointer ,struct (,struct-ptr) ,release-function)) | |
(c-define-type ,borrowed-ptr (pointer ,struct (,struct-ptr))) | |
,@string-compat-types | |
(define ,(string->symbol (string-append struct-str "-ptr?")) | |
(lambda (obj) | |
(and (foreign? obj) (equal? (foreign-tags obj) '(,struct-ptr))))) | |
,@(apply append | |
(map (lambda (m) | |
(let* ((member-name (symbol->string (car m))) | |
(member-type (cdr m)) | |
(getter-name | |
(string-append struct-str "-" member-name)) | |
(setter-body | |
(cond ((member member-type string-types) | |
(string-setter-body member-name)) | |
(else | |
(string-append | |
"___arg1->" | |
member-name | |
" = ___arg2;" | |
"\n" | |
"___return;" | |
"\n"))))) | |
`((define ,(string->symbol getter-name) | |
(c-lambda | |
(,struct-ptr) | |
,member-type | |
,(string-append | |
"___return(___arg1->" | |
member-name | |
");"))) | |
(define ,(string->symbol | |
(string-append getter-name "-set!")) | |
(c-lambda | |
(,struct-ptr ,member-type) | |
void | |
,setter-body))))) | |
members)) | |
(define ,(string->symbol (string-append "malloc-" struct-str)) | |
(c-lambda | |
() | |
,struct-ptr | |
,(string-append | |
"struct " | |
struct-str | |
"* var = (struct " | |
struct-str | |
" *) malloc(sizeof(struct " | |
struct-str | |
"));" | |
"\n" | |
"if (var == NULL)" | |
"\n" | |
" ___return (NULL);" | |
"\n" | |
"memset(var, 0, sizeof(struct " | |
struct-str | |
"));" | |
"___return(var);"))) | |
(define ,(string->symbol (string-append "ptr->" struct-str)) | |
(c-lambda (,struct-ptr) ,struct "___return(*___arg1);")) | |
(define ,(string->symbol | |
(string-append "malloc-" struct-str "-array")) | |
(c-lambda | |
(unsigned-int32) | |
,(if string-compat-required? shallow-ptr struct-ptr) | |
,(string-append | |
"struct " | |
struct-str | |
" *arr_var=(struct " | |
struct-str | |
" *) malloc(___arg1*sizeof(struct " | |
struct-str | |
"));" | |
"\n" | |
"if (arr_var == NULL)" | |
"\n" | |
" ___return (NULL);" | |
"\n" | |
"memset(arr_var, 0, ___arg1*sizeof(struct " | |
struct-str | |
"));" | |
"\n" | |
"___return(arr_var);"))) | |
(define ,(string->symbol (string-append struct-str "-array-ref")) | |
(c-lambda | |
(,struct-ptr unsigned-int32) | |
,borrowed-ptr | |
"___return (___arg1 + ___arg2);")) | |
(define ,(string->symbol (string-append struct-str "-array-set!")) | |
(c-lambda | |
(,struct-ptr unsigned-int32 ,struct-ptr) | |
void | |
"*(___arg1 + ___arg2) = *___arg3; ___return;"))))) | |
(c-declare "#include <stdlib.h>") | |
(c-declare "#include <string.h>") | |
(c-declare "#include <errno.h>") | |
(c-declare "static ___SCMOBJ ffi_free (void *ptr);") | |
(c-declare | |
"#ifndef ___HAVE_FFI_U8VECTOR\n#define ___HAVE_FFI_U8VECTOR\n#define U8_DATA(obj) ___CAST (___U8*, ___BODY_AS (obj, ___tSUBTYPED))\n#define U8_LEN(obj) ___HD_BYTES (___HEADER (obj))\n#endif") | |
(namespace | |
("drewc/gurf/surf#" | |
evalscript | |
Client-targeturi | |
Client-title | |
Client-next | |
Client* | |
Client | |
updatetitle | |
gtk_surf_iteration | |
showview | |
loaduri | |
clients | |
newclient | |
setup)) | |
(c-declare "#include \"surf/surf.c\"") | |
(define-c-lambda setup () void "setup") | |
(c-declare "int ____nofreeclient(Client *c){ return 0;}") | |
(define-c-struct | |
Client | |
((title . char-string) | |
(targeturi . char-string) | |
(next . Client-borrowed-ptr*)) | |
"____nofreeclient") | |
(define-c-lambda newclient (Client*) Client* "newclient") | |
(define-c-lambda | |
showview | |
(Client*) | |
void | |
"showview(NULL, ___arg1); ___return;") | |
(define-c-lambda | |
loaduri | |
(Client* char-string) | |
void | |
"Arg arg; arg.v = ___arg2 ; loaduri(___arg1, &arg); ___return;") | |
(define-c-lambda updatetitle (Client*) void "updatetitle") | |
(define-c-lambda | |
evalscript | |
(Client-borrowed-ptr* char-string) | |
void | |
"evalscript(___arg1, \"%s\", ___arg2); ___return;") | |
(define-c-lambda clients () Client* "___return(clients);") | |
(define-c-lambda | |
gtk_surf_iteration | |
() | |
bool | |
"gboolean res = g_main_context_pending(NULL);\n while (g_main_context_pending(NULL)) {\n g_main_context_iteration(NULL, FALSE);\n }; ___return(res);") | |
(c-declare | |
"#ifndef ___HAVE_FFI_FREE\n#define ___HAVE_FFI_FREE\n___SCMOBJ ffi_free (void *ptr)\n{\n free (ptr);\n return ___FIX (___NO_ERR);\n}\n#endif")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment