From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Taylan Kammer Newsgroups: gmane.lisp.guile.user Subject: Re: Difficulty integrating with Swift/Objective-C Date: Sun, 5 Sep 2021 10:26:49 +0200 Message-ID: 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="23747"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0 To: paul , guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Sun Sep 05 10:27:14 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 1mMnUf-00062l-J3 for guile-user@m.gmane-mx.org; Sun, 05 Sep 2021 10:27:13 +0200 Original-Received: from localhost ([::1]:47446 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mMnUd-0003Hx-TE for guile-user@m.gmane-mx.org; Sun, 05 Sep 2021 04:27:11 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:33328) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mMnUO-0003Hl-GC for guile-user@gnu.org; Sun, 05 Sep 2021 04:26:56 -0400 Original-Received: from mail-wr1-x430.google.com ([2a00:1450:4864:20::430]:35721) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mMnUM-0007Df-DL for guile-user@gnu.org; Sun, 05 Sep 2021 04:26:56 -0400 Original-Received: by mail-wr1-x430.google.com with SMTP id i6so4979253wrv.2 for ; Sun, 05 Sep 2021 01:26:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=3NN6BmnZb6Gkjd5du4BNa6tCspOv+cko8oR84UDMAKQ=; b=LnxgaZAdT8Nr0cVOFI/Wm3qhFxWicUzoiklSmw3qpzjiXUQ0C5SLV60V0mXRRbfpIn Ogfq7YYsw250En6Hd2moCFfYqDCV4KKSNmWQbmxlZ7CWbtRk0W3CJwiJTHd/dFsoMFU5 vsLgFVmIpF2hG1We6hIMFwr8atSQM7XwWhHQHmu4c0Pc2NYcH1alQp3C7xskuWu5QnXN wdmiPv99Rzse+F7Ll4CB1JbADero++EGtEi43gfShZpTb1oZIVacSHjeiFOmyvKS1+mb RTogx8bgDIBititpsM+Jj7kvmnYJwxLodnwbEfGn3w2KFeeggSVcn6pqU4bhKY9Lb6Jg Q4tQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=3NN6BmnZb6Gkjd5du4BNa6tCspOv+cko8oR84UDMAKQ=; b=qLyypJWw7yIsNIdsJ/znBuEyGMelLfo4S7gW9rUUvPYEA00k7MCdj/XganbJjTjPbC pitXM2uGU4wbUGgkuMj7w3l4MTOCKRfP7y+7MijuDBElRlCG1S73R10doxleMfua/I8c sdhpLsMSCMn8AGrHbJ715/fmMP6poJJGUBuueJMIja+ipxY/TUPAIa1fVsw9L10w4n1T b1GI9YetiUcUxhFvxPp6W5KIqPY8dzAChhhtUoUBiTPzM4O5Fl1ITJrVT/w7f+v3va8M b9leIzvpsN/skz4GOokpO1//+9+8cPtztXN3VOyZqP2LaiJemrdhokN2wEbYNQY9ZscP l/Qg== X-Gm-Message-State: AOAM53237peqRwteIfsFaNKzYiMCUH+ooX6fpxCw/66R/HVvwWhr+81T ULiLPMMTuNezZ6EicrafAeL4D2wk1WI= X-Google-Smtp-Source: ABdhPJzzUJDwn2R3wxxW/Mnn7h9UyoUIepCgJdm6z8MDGIVQYkbGy58SKWptnJ4HVAjG4fyQbp6hOg== X-Received: by 2002:adf:d1ab:: with SMTP id w11mr7211968wrc.372.1630830412793; Sun, 05 Sep 2021 01:26:52 -0700 (PDT) Original-Received: from [192.168.178.20] (b2b-109-90-125-150.unitymedia.biz. [109.90.125.150]) by smtp.gmail.com with ESMTPSA id v62sm4001593wme.21.2021.09.05.01.26.52 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 05 Sep 2021 01:26:52 -0700 (PDT) In-Reply-To: Content-Language: en-US Received-SPF: pass client-ip=2a00:1450:4864:20::430; envelope-from=taylan.kammer@gmail.com; helo=mail-wr1-x430.google.com X-Spam_score_int: -58 X-Spam_score: -5.9 X-Spam_bar: ----- X-Spam_report: (-5.9 / 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_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:17713 Archived-At: On 05.09.2021 08:03, 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? 🙏 > > All the best, > paul > > 1. https://mail.gnu.org/archive/html/bug-guile/2021-03/msg00012.html > Hi Paul, To narrow down the issue, I'd attempt a few things, in order: 1. Compile only the C code, adding a main() function, just to make sure the OS and the chosen Guile version and such are working fine with each other. 2. Compile pure Objective-C code, calling that run_guile() function firstly directly from the main() function in main.m of the Objective-C program, and commenting out the NSApplicationMain() call that would initialize Apple's application framework. 3. See if reactivating the NSApplicationMain() call causes problems. (It should be called *after* the Guile initialization.) 4. See if you can use Guile's C functions from -applicationDidFinishLaunching: e.g. by doing: scm_c_eval_string("(begin (display 'HelloWorld) (newline))") If that works, we now have an Objective-C + Guile application, and want to move to using Swift instead. This is where my Apple knowledge hits its limits because I never used Swift. :-) But I guess Swift should have something equivalent to the main() function of C and Objective-C, and calling Guile initialization from there might do the trick. If you hit a problem in step 3 or 4, then it could mean that Guile and Cocoa are somehow incompatible as they both want to apply some magic to the C program while initializing themselves. I wouldn't know how to approach that issue. -- Taylan