abs-lang / abs

Home of the ABS programming language: the joy of shell scripting.
https://www.abs-lang.org
MIT License
510 stars 35 forks source link

Possbile plugin system? #480

Open Iskander0 opened 1 year ago

Iskander0 commented 1 year ago

I extended ABS for myself to be able to call functions from go plugins. Perhaps something like this can be considered for main.

Simplest plugin example :

package main

func Add(argc int32, argv []interface{}) interface{} {
    x := argv[0].(float64)
    y := argv[1].(float64)
   return x + y // Returned values automatically converted to ABS data types
}

Compiled with go build -buildmode=plugin, can be called from an ABS script like this:

# arg1 : path to compiled plugin ; arg2 : Plugin gets a name which is used in execShared
importShared("plugin.so", "plugin") /

# arg1: Name from arg2 of importShared ; arg2 : Name of exported functions from the go plugin ; arg3 : Array which gets passed to the go function called, basic types automatically converted.
execShared("plugin", "Add", [1, 2]) // returns 3

The implementation is kind of bad, as it would be nice to be able to get a library "object" instead of having to "name" all loaded plugins, but I like the simplicity of not having to worry about ABS-specific or go-specific types (as is the case with something like luajit ffi, or python C extensions). I can share some code if anyone is interested.

odino commented 1 year ago

hey @Agathon1 -- apologies, missed this one! 😊

I would be +1 on the ability to load external plugins. One thing to consider, the public interface could probably be looking a little different eg. we could make this work with x = require('plugin.so') and then be able to call x.something. I don't have a strong sense on what we need to do to get this working, but I'd be happy to engage with you if you wished to send a PR / proposal.