Comments (6)
@IngwiePhoenix Glad that you figured out most the stuff :)
The reason you can't get a return value is a consequence of Elvish's language semantics: there's simply no concept of return values in Elvish. Instead, Elvish has outputs, and to "return" something you'll need to write it out with put
(for value outputs) or echo
(for byte output) or similar commands and capture it.
I've added a doc that specifically answers this question: https://github.com/elves/elvish/blob/master/docs/elvish-as-library.md (beware that the godoc pages on pkg.go.dev takes a while to refresh). The examples for Evaler.Eval in particular shows how to get "return" values from Elvish code.
from elvish.
So if I want a real return value, I would have to execute the snippet and then make the code set a global value which I can then pick off the context - so, something remotely similiar to
$?
. Hey, works for me - simple enough. :)
That, or capture the output if you don't need it for other purposes (and like in Elvish itself, you can pass structured values using builtins like put
).
Thanks for the additions!
Np!
from elvish.
Embedding any code written in Go inside an app written in another language is problematic. Some of the challenges are due to the Go runtime, how types are defined in each language, and memory ownership. It also creates a tight coupling that causes its own problems. There are other challenges such as how to translate Elvish exceptions over the FFI. One relatively recent article that discusses how to do this is https://www.dolthub.com/blog/2023-02-01-embedding-go-in-c/. I can't speak for the project owner but I doubt this will ever be done and certainly not in the near future. There are just too many other enhancements that could be made which would add considerably more value. Rather than attempting to embed Elvish in a different program using FFI it is likely to be simpler to use an RPC mechanism to interact with an Elvish shell.
from elvish.
Oh - I was more talking about something like
import "src.elv.sh/pkg/..."
func main() {
var context = Elvish.create()
context.run(...)
}
But I guess I have to dig through the source more and start with cnd/elvish
to find that. Still, thanks for the blog post, that was quite interesting!
from elvish.
Dug around a while and came to this:
- An
Evaler
(...pkgs/eval
) and aParser
(...pkgs/parser
) are needed. elvish
uses this to set up it'sEvaler
:Lines 99 to 132 in cee682c
- This here illustrates how to best make a
Parser
:Line 48 in cee682c
So, whilst there is no "made for embedding" API, it is actually quite easy to set this up - and as it is mostly contained in structures, it should? be threadsafe.
However, there is no way to "return" a value from the script to the parent, as far as I can tell. That said, there is some interesting stuff here: https://github.com/elves/elvish/blob/cee682c489925b47d8d46f9afdc31acc19557d10/pkg/eval/vals/struct_map.go
So I imagine it would be possible to just bind a function to the Evaler
that would act as a "return" to the host by wrapping the actual code - which, in the tool I want to make, wouldn't be a big problem, since most of the scripts run are rather short snippets, so wrapping them isn't a big deal.
Would be neat to have a plain pkgs/embed
module that simplifies this a little; might make it myself in fact, this doesn't seem too difficult - at least, at present. o.o
from elvish.
So if I want a real return value, I would have to execute the snippet and then make the code set a global value which I can then pick off the context - so, something remotely similiar to $?
. Hey, works for me - simple enough. :)
Thanks for the additions!
from elvish.
Related Issues (20)
- Support the kitty keyboard protocol
- Mapping `$edit:close-mode~` has no effect HOT 10
- `edit:complete-filename` call without arguments crashes elvish HOT 1
- Unique symbols HOT 1
- Disable flow control by default HOT 1
- vscode-neovim doesn't work with Elvish as default shell HOT 15
- Race condition when starting multiple instances simultaneously HOT 2
- RSS support for the blog
- Support semantic prompts (OSC 133) HOT 1
- Directory Elvish is launched from doesn't seem to be added to location history HOT 10
- RISC-V binaries HOT 4
- "Command not found" error when running a batch script with space in path with an argument with space HOT 16
- API to change the in-command colours? HOT 3
- Cursor and prompt control in function to avoid prompt flick HOT 9
- document and change behavior of `doc:find` with no arguments
- Indexing into a list of maps
- listbox_window: integer divide by zero HOT 4
- Add `&key` option to `compact`.
- Are there any good examples for custom completion script?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from elvish.