From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Chris Vine Newsgroups: gmane.lisp.guile.user Subject: Re: Difficulty integrating with Swift/Objective-C Date: Sun, 5 Sep 2021 11:56:46 +0100 Message-ID: <20210905115646.dcb786924e77fb3198e5271b@gmail.com> References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="14013"; mail-complaints-to="usenet@ciao.gmane.io" To: guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Sun Sep 05 12:56:09 2021 Return-path: Envelope-to: guile-user@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mMpon-0003WV-2c for guile-user@m.gmane-mx.org; Sun, 05 Sep 2021 12:56:09 +0200 Original-Received: from localhost ([::1]:42274 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mMpol-0008Hx-Lu for guile-user@m.gmane-mx.org; Sun, 05 Sep 2021 06:56:07 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:51032) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mMpoY-0008H2-OB for guile-user@gnu.org; Sun, 05 Sep 2021 06:55:54 -0400 Original-Received: from mail-wm1-x32f.google.com ([2a00:1450:4864:20::32f]:37388) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mMpoW-0005nb-PI for guile-user@gnu.org; Sun, 05 Sep 2021 06:55:54 -0400 Original-Received: by mail-wm1-x32f.google.com with SMTP id c8-20020a7bc008000000b002e6e462e95fso2799766wmb.2 for ; Sun, 05 Sep 2021 03:55:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=date:from:to:cc:subject:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=mLhO5oI69L+NztTWPujd6wa0Cdep6uJ1grwbichlFk0=; b=cjb9td0S8slYW6GIHJMJ0IvmQkYpOj/hehqlSxsvbnsXDuruRKsTQbFbSNYzlziU+w HghYb7/MWyOUhrqq/FPuO6CKM5+l3l+kasnCUG90ygMeEL0QkNb7CzYZqUAR/NkGwDXK 3x5pE6JjanRC6bpVCHJF1GWRx0PkxnKYczfuD8xBAKF8Lc+17wwiXc+nZi0tm6gt4y/J lwQ/xFDb3j5HawCRsFvJWDCnBFLtxpJnjsVRE0jYPovMUZ1KYaCTuQE4zypiSQUWWSMx yuTUZF/+y0tVLCtRfQtPFYKSlhq0pwiFdioHpITsi9wkNMrFJoMF7jhpa4936455p0Ei zlIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=mLhO5oI69L+NztTWPujd6wa0Cdep6uJ1grwbichlFk0=; b=RnDT2PvCtrAZLCplyY7LmLOLIcp4UuN7WTV8/QbB40z9+4dtawBG53UVeTfhUU2+OY QKaLap9RsGpgFhmBxWrFb53R1UCG+X7Oj65PhUtCmvQGykqlQOsQGwn/yO58NNITaIL6 LDFBtDSpvAr/eEu8Nb/M/4vDpRJfdjkSbzehH4sVIcCfy76HEtytp+6ul1XFyZ75nXFm hFF8vKagX8k15tNDM+zOw2naITBIq0xmEtgztS+L9pVaKB3JoASsZo6yNiXhDP+TsHmD AW9gE+o3v2Wo3xPOLkdgf2blP3qpWNOuk+RcmCNsZsMEaG1Y+YBuYJeIT4yl9929yj5i g8AA== X-Gm-Message-State: AOAM531q1rNIvbQFO09AmEDo//rlLCj02e0cZwCw8tM+Y87EdWW7ZgYP G7scH4vb/fy75XlbiRl5kqAJTY6oRzs= X-Google-Smtp-Source: ABdhPJzG28za5kLYe6hFpepqFNWCxyGZ8BPDJvGSFFSI+3vYpSrfU/HwhhdHGcQqpacf/ZwCtHi/JA== X-Received: by 2002:a7b:cb8c:: with SMTP id m12mr6507447wmi.77.1630839349197; Sun, 05 Sep 2021 03:55:49 -0700 (PDT) Original-Received: from bother.homenet ([2.27.184.177]) by smtp.gmail.com with ESMTPSA id q13sm4248959wmj.46.2021.09.05.03.55.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Sep 2021 03:55:48 -0700 (PDT) Original-Received: from bother.homenet (localhost [127.0.0.1]) by bother.homenet (Postfix) with SMTP id 66A62260879; Sun, 5 Sep 2021 11:56:46 +0100 (BST) In-Reply-To: X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.33; x86_64-unknown-linux-gnu) Received-SPF: pass client-ip=2a00:1450:4864:20::32f; envelope-from=vine35792468@gmail.com; helo=mail-wm1-x32f.google.com X-Spam_score_int: -56 X-Spam_score: -5.7 X-Spam_bar: ----- X-Spam_report: (-5.7 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, NICE_REPLY_A=-3.832, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.io gmane.lisp.guile.user:17715 Archived-At: On Sun, 05 Sep 2021 16:03:24 +1000 paul wrote: > Good day, > > I have an existing app which is written in Swift and runs on macOS > 10.15. I would like to provide users a way of customising the app > (initially just simple things like modifying keybindings for > example, later hopefully more) and as a keen Emacs user, i'm > inspired by the idea of providing a Schemey/Lispy interface for > such extensions. Guile looks like it'd be great for this. If i > understand correctly, it'd be best if i could bundle the Guile > runtime as a static library with the app, and call out to it to > evaluate user-provided code. I haven't thought deeply about this > interface yet; i thought i'd get a proof-of-concept working first. > I wonder if i might humbly ask for some guidance on how to get it > working, because after a couple of days i seem to have failed. > I'm no C/threads/low-level guru, so my apologies if i'm doing > something very dumb. > > I had some difficulty getting my app to compile against Guile, but > i eventually managed to link against a version of Guile installed > with Homebrew (guile: stable 3.0.7 (bottled)), however when trying > to boot it up i seemed to run into the same issue described by > Jeffrey Walton [1]. My app would boot, and as soon as it hit the > Guile initialisation calls, it would error with "allocating JIT > code buffer failed: Permission denied, jit.c:5804: fatal: > assertion failed". While that person seems to imply the problem > is with Apple's M1 silicon, i'm actually running an older machine > (2.9 GHz Dual-Core Intel Core i5, macOS 11.5.2). I then managed > to get further by downloading the Guile release tarball version > 3.0.7 and and building with `./configure --enable-jit=no`; this > got me a bit further, however it still didn't work: i think it is > because some assumption Guile has about the thread it runs on, or > when it's invoked, is violated.. but i'm unsure how to find out. > > What i currently have, is this snippet. It's being called from > Swift land, in the `applicationDidFinishLaunching(_ aNotification: > Notification)` function. As far as i can tell, that _is_ the main > thread. > > ``` > #include "libguile.h" > > static void* register_functions (void* data) > { > SCM test = scm_c_eval_string("(+ 3 5)"); > int foo = scm_to_int(test); > printf("foo = %d\n", foo); > > return NULL; > } > > void run_guile() { > printf("hello from C, before Guile\n"); > scm_init_guile(); > //scm_with_guile(®ister_functions, NULL); // i've tried > only having this line uncommented, too, but that also causes > immediate crashes > //scm_shell(0, NULL); > } > ``` > > This compiles fine, and i see the "hello from C" line printed, but > then it crashes. The error seems to vary, here are some i've > seen: > > 1. "Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)" at line 182 of > pairs.h, > 2. "Pre-boot error; key: misc-error, args: ("%search-path" "path > is not a proper list: ~a" (("/usr/local/share/guile/3.0" > "/usr/local/share/guile/site/3.0" "/usr/local/share/guile/site" > . #)) #f)", "Thread 1: signal > SIGABRT", line 260 of throw.c > 3. "Thread 1: EXC_BAD_ACCESS (code=1, address=0x9)", at line 585 > of weak-set.c. > 4. I've also sometimes seen this one, > https://lists.gnu.org/archive/html/emacs-bug-tracker/2020-01/msg00365.html, > although perhaps that's indeed related to closed stdout. > > Because these errors are different all the time i guess it's some > race condition or threading issue? I wonder if someone knows an > avenue i can attempt to use to debug what's going on? 🙏 You appear to want to run scheme code as an extension language, via the guile VM, in a program written for MacOS in the Swift language. I know nothing about the Swift language and whether it has a sufficient C-like FFI to achieve this, but you may get some inspiration from the following, which enables scheme code to be run as an extension language within a C++ program: https://sourceforge.net/p/cxx-gtk-utils/git/ci/master/tree/c++-gtk-utils/extension.h However there are various issues. First, guile's exceptions (based on C's longjmp()/setjmp() and cognates) are almost certainly not implemented in the same way as Swift's exceptions (assuming Swift has exceptions), which means amongst other things that, if Swift has something akin to destructors/deinitialization, you should not allow guile exceptions to leave Swift objects undestroyed were guile to jump out of local scope on raising an exception (scm_dynwind_begin(), scm_dynwind_end and scm_dynwind_unwind_handler can be useful here if you are able to manage your Swift objects manually in C). Nor more generally should you allow Swift exceptions (if Swift has them) to escape out of a guile scm_dynwind_begin()/scm_dynwind_end() pair, or you will stop guile's resource management working correctly. Secondly, there are some issues relating to the fact that guile's 'make-fresh-user-module' and 'load' procedures leak memory, which can be problematic if all tasks run as scheme code are not to share the same top level. Thirdly you will need to cater for translating guile return values such as lists or vectors into containers that Swift can work with, and vice versa. This is all pretty well documented in the C++ code mentioned above, but it is not for the faint hearted. Chris