Skip to content

Commit

Permalink
[starlark] Add initial support for embedded Starlark interpreter. [se…
Browse files Browse the repository at this point in the history
…e issue #29]
  • Loading branch information
cipriancraciun committed Feb 13, 2021
1 parent af96624 commit f71e120
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 1 deletion.
38 changes: 38 additions & 0 deletions sources/lib/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,33 @@ func prepareExecution_0 (
_interpreterScriptBuffer.WriteString (_scriptletBody)
}

case "<starlark>" :
if _libraryUrl != "" {
_interpreterExecutable = _selfExecutable
_interpreterArgument0 = "[z-run:library]"
_interpreterArguments = append (
_interpreterArguments,
_scriptletInterpreterArguments ...,
)
_interpreterArguments = append (
_interpreterArguments,
fmt.Sprintf (":: %s", _scriptletLabel),
)
_interpreterScriptUnused = true
} else {
_interpreterExecutable = _selfExecutable
_interpreterArgument0 = fmt.Sprintf ("[z-run:starlark] [%s]", _scriptletLabel)
_interpreterArguments = append (
_interpreterArguments,
_scriptletInterpreterArguments ...,
)
_interpreterArguments = append (
_interpreterArguments,
fmt.Sprintf ("/dev/fd/%d", _interpreterScriptInput),
)
_interpreterScriptBuffer.WriteString (_scriptletBody)
}

case "<menu>" :
_interpreterExecutable = _selfExecutable
_interpreterArgument0 = fmt.Sprintf ("[z-run:menu] [%s]", _scriptletLabel)
Expand Down Expand Up @@ -607,6 +634,17 @@ func executeScriptlet (_library LibraryStore, _scriptlet *Scriptlet, _fork bool,
} else {
return _error
}
case "<starlark>" :
if _error := executeStarlark (_library, _scriptlet, _context); _error == nil {
if ! _fork {
os.Exit (0)
panic (0x44d7fe22)
} else {
return nil
}
} else {
return _error
}
}

var _libraryFingerprint string
Expand Down
19 changes: 18 additions & 1 deletion sources/lib/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func parseInterpreter (_scriptlet *Scriptlet) (*Error) {
switch _scriptlet.Interpreter {
case "<detect>" :
// NOP
case "<exec>", "<print>", "<template>", "<menu>", "<go>", "<go+>" :
case "<exec>", "<print>", "<template>", "<starlark>", "<menu>", "<go>", "<go+>" :
return nil
case "<bash>", "<bash+>", "<python3+>" :
return nil
Expand Down Expand Up @@ -342,6 +342,23 @@ func parseInterpreter_0 (_scriptletLabel string, _scriptletBody_0 string, _scrip
_interpreterArgumentsExtraAllowed = true
_interpreterEnvironment = nil

} else if strings.HasPrefix (_scriptletHeader, "<starlark>") {

_scriptletHeader = _scriptletHeader[10:]
_scriptletHeader = strings.Trim (_scriptletHeader, " ")

if _scriptletHeader != "" {
_errorReturn = errorf (0xc7d23157, "invalid header for `%s` (starlark with arguments)", _scriptletLabel)
return
}

_interpreter = "<starlark>"
_interpreterExecutable = ""
_interpreterArguments = nil
_interpreterArgumentsExtraDash = false
_interpreterArgumentsExtraAllowed = true
_interpreterEnvironment = nil

} else if strings.HasPrefix (_scriptletHeader, "<print>") {

_scriptletHeader = _scriptletHeader[7:]
Expand Down
16 changes: 16 additions & 0 deletions sources/lib/premain.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ func PreMain () () {
_argument0 = "[z-run:print]"
} else if strings.HasPrefix (_argument0, "[z-run:template] ") {
_argument0 = "[z-run:template]"
} else if strings.HasPrefix (_argument0, "[z-run:starlark] ") {
_argument0 = "[z-run:starlark]"
}


Expand Down Expand Up @@ -318,6 +320,9 @@ func PreMain () () {
case "[z-run:template]" :
// NOP

case "[z-run:starlark]" :
// NOP

case "[z-run:scriptlet]" :
// NOP

Expand Down Expand Up @@ -391,6 +396,10 @@ func PreMain () () {
_argument0 = "[z-run:template]"
_arguments = _arguments[1:]

case "--starlark" :
_argument0 = "[z-run:starlark]"
_arguments = _arguments[1:]

default :
_argument0 = "[z-run]"
}
Expand Down Expand Up @@ -453,6 +462,13 @@ func PreMain () () {
panic (0x32241835)
}

case "[z-run:starlark]" :
if _error := starlarkMain (_executable, _arguments, _environment); _error != nil {
panic (abortError (_error))
} else {
panic (0xd6f5b038)
}

case "[z-run:menu]" :
if _error := menuMain (_executable, _arguments, _environment); _error != nil {
panic (abortError (_error))
Expand Down
108 changes: 108 additions & 0 deletions sources/lib/starlark.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@


package zrun


import "os"

import "go.starlark.net/starlark"




func starlarkMain (_selfExecutable string, _arguments []string, _environment map[string]string) (*Error) {

if len (_arguments) < 1 {
return errorf (0xd776bfb0, "invalid arguments")
}

var _sourcePath = _arguments[0]
_arguments = _arguments[1:]

var _sourceBody string
if _, _data, _error := loadFromFile (_sourcePath); _error == nil {
_sourceBody = string (_data)
} else {
return _error
}

_error := executeStarlark_0 (
_sourceBody,
_arguments,
_environment,
_selfExecutable,
"",
"",
"",
nil,
)
if _error != nil {
return _error
}

os.Exit (0)
panic (0x340179ee)
}




func executeStarlark (_library LibraryStore, _scriptlet *Scriptlet, _context *Context) (*Error) {

if _scriptlet.Interpreter != "<starlark>" {
return errorf (0x8044a656, "invalid interpreter")
}

_libraryUrl := _library.Url ()
_libraryFingerprint := ""
if _libraryFingerprint_0, _error := _library.Fingerprint (); _error == nil {
_libraryFingerprint = _libraryFingerprint_0
} else {
return _error
}

_extraFunctions := make (map[string]interface{}, 16)

return executeStarlark_0 (
_scriptlet.Body,
_context.cleanArguments,
_context.cleanEnvironment,
_context.selfExecutable,
_context.workspace,
_libraryUrl,
_libraryFingerprint,
_extraFunctions,
)
}




func executeStarlark_0 (
_source string,
_arguments []string,
_environment map[string]string,
_selfExecutable string,
_workspace string,
_libraryUrl string,
_libraryFingerprint string,
_extraFunctions map[string]interface{},
) (*Error) {

_thread := & starlark.Thread {
Name : "scriptlet",
Print : func (_thread *starlark.Thread, _message string) () {
logf ('>', 0x98499c0a, "%s", _message)
},
Load : nil,
}

_builtins := starlark.StringDict {}

if _, _error := starlark.ExecFile (_thread, "<scriptlet>", _source, _builtins); _error != nil {
return errorw (0x4027d940, _error)
}

return nil
}

0 comments on commit f71e120

Please sign in to comment.