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: Mon, 6 Sep 2021 17:28:44 +0100 Message-ID: <20210906172844.ed1fd1affdd8c64e73069705@gmail.com> References: <20210905115646.dcb786924e77fb3198e5271b@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="23907"; 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 Mon Sep 06 18:28:03 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 1mNHTX-0005zN-8C for guile-user@m.gmane-mx.org; Mon, 06 Sep 2021 18:28:03 +0200 Original-Received: from localhost ([::1]:39568 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mNHTV-0005o5-A2 for guile-user@m.gmane-mx.org; Mon, 06 Sep 2021 12:28:01 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:42100) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mNHTK-0005nu-Kl for guile-user@gnu.org; Mon, 06 Sep 2021 12:27:50 -0400 Original-Received: from mail-wr1-x42c.google.com ([2a00:1450:4864:20::42c]:39934) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mNHTI-0006UY-Nt for guile-user@gnu.org; Mon, 06 Sep 2021 12:27:50 -0400 Original-Received: by mail-wr1-x42c.google.com with SMTP id z4so10591712wrr.6 for ; Mon, 06 Sep 2021 09:27:47 -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=i3Znrh+yGY+UyDaYhE7l2ReGbf7Y+YUhfIG6waPjWGU=; b=QVxuuncosYn/GEy9awLWBZLzgxtHHMwpYSzngYFvqU+MSwBnvukz49ZWPRzPSa85R8 Hyb4vS2G9aht0NorGv2j42fINiNl5SEBgDQo24LTAHc4uxDPf7/JiplBH2mjWqPlNIAB Ir81iCR4/Esl1XH8PEPql/nHT21Pni0HZ1oGjNTEkqJMIC0DO9/B5oZ2pTWlaKAUw+/b KmUnyBafnGDrVT4uXj7Lbm3eY8hRWbjf5EaGebfOOVmAhYDP+mystwtIAGSDXwRCZ16Z Qn8ebUhlYfwLaFzqTejpqRw4maEco6LiyHlCdQZ37XcO81w/1ULJy+BXHzthiGUjrZ8f sMaw== 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=i3Znrh+yGY+UyDaYhE7l2ReGbf7Y+YUhfIG6waPjWGU=; b=FNcRA0d/5tCwa5VUQXnJejw/6x0TDLDqDad+rZZ5Ftu6PcxlaUdgunc+0AArstI7oC C7B7KTIg61MEseMmxTxNM5s6FGOgR+L7qk1sfpy7bfD+tF1brtAPZ3GMo2IImyOpj/JK IsXF7UxQKkaNgBv697PhYhoringMdpwKUb2yqmO0PzjGQ/7j+RKK0JMrCKd0TY6aZffx Qx9coN06Qp6gr0vDdwuffT8tglAcWpw269V258hmcBTXzUpLA8jVXUHQTzbsFFkJV/Px spzEOkeLYNkXVphssVmI5yK+XfZbsP9O6qtByy/btV0J6ELbilJGt5zCIzmeOYaPSBXu odyg== X-Gm-Message-State: AOAM5313C2+YMahnvlc3A9JuWfjSUJDS4PlVvjcZf4xVzOrlOIZUf8Ev eeVHfMl19OcYHU2TEOm+1LZt6EcuMfM= X-Google-Smtp-Source: ABdhPJz++DaKmiZDeMSb60AtD3oVFADXPknjXQPbJRUPwuGdosxR3ULwKGNN5XYO7SPHAjI8g9OafA== X-Received: by 2002:a05:6000:1569:: with SMTP id 9mr14313611wrz.343.1630945666693; Mon, 06 Sep 2021 09:27:46 -0700 (PDT) Original-Received: from bother.homenet ([2.27.184.177]) by smtp.gmail.com with ESMTPSA id z1sm7859014wmi.34.2021.09.06.09.27.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Sep 2021 09:27:46 -0700 (PDT) Original-Received: from bother.homenet (localhost [127.0.0.1]) by bother.homenet (Postfix) with SMTP id 121852612DD; Mon, 6 Sep 2021 17:28:45 +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::42c; envelope-from=vine35792468@gmail.com; helo=mail-wr1-x42c.google.com X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 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=-2.332, 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:17720 Archived-At: On Mon, 06 Sep 2021 20:26:53 +1000 paul wrote: > Hey Chris, > > On 2021-09-05 at 20:56 AEST, quoth Chris Vine > : > > 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. > > This is exactly right. > > > 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. > > I'll definitely take a look at the codebase you link; thank you. > Although i am probably on the fainter-hearted side, i have a bit > of hope it might be feasible, since my project actually already > includes a "core" implemented in Rust and included as a > statically-linked library. I'm able to call out to Rust functions > i've marked as `extern C`, and so while i don't fully understand > the mechanism and the implications for threading and exceptions, > so far it has worked well in that limited case. I'm hoping that i > can make something similar work in Guile. I think my needs are > fairly modest - initially my ambition is to allow the user to > rebind shortcut keys of my app using a Guile script. We'll see > what else i can come up with later. > > Thanks for the info though, i'll definitely refer back if i run > into issues! If you cut the code that I referred to down to its bare essentials, it does what I think you have now arrived at yourself: calling scm_eval_string() (in that code's case, actually scm_eval_string_in_module()), via scm_with_guile(). It is scm_with_guile() which actuates the guile VM within your program and under which your scheme code is evaluated. You don't need to (and really shouldn't) execute another main() procedure within your program to evaluate the scheme code, and doing that would make integrating it with your Swift code much more difficult. scm_with_guile() and scm_eval_string() will execute guile code within your program as if it were any other C function. However the complications arise when you try to interface the scheme value returned by scm_eval_string (and so by scm_with_guile()) into your Swift code - that is, convert SCM values to Swift values, or you need to deal with guile or Swift exceptions. Obviously the first of those is less relevant if you are only executing scheme code for its side effects. There are also important issues to be aware of if you intend to execute guile code in more than one thread or you don't want every task called up by scm_with_guile() to share the same top level. Looking at the code I posted should be useful for dealing with those issues. But the essentials of what you are doing seems right. Chris