fubark / cyber

Fast and concurrent scripting.
https://cyberscript.dev
MIT License
1.16k stars 38 forks source link

Attempting to print the output of return value from execCmd() (value.out) giving 'rawstring' #54

Closed MostHated closed 1 year ago

MostHated commented 1 year ago

Hey there. I am attempting to print the output of return value from execCmd() (value.out), but it keeps giving 'rawstring(n)'. I tried using value.out.utf8() and various other things, but none of them seem to give what I expected as the result. Is the 'out' parameter from the result object of the execCmd call not what I am thinking it should be?

I feel that I must be missing something, or not properly understanding something?

Thanks, -MH


#!/usr/local/bin/cyber
-- test_script.cy hello

import os 'os'
args = os.args()

runArgs(args[2]) -- hello

func runArgs(arg):
    cmd = [ '/bin/bash', '-c', 'echo "{arg}"' ]
    res = execCmd(cmd)

    if res.exited != 0:
        print res.out
        print res.err
    else:
        print res.out    -- rawstring (5)
        print res.out[0] -- r

        t = res.out.utf8() -- This gives the below error 
        print t            -- Expecting 'hello' printed

        -- panic: `utf8` is either missing in `string` or the call signature: utf8(self, 0 args) is unsupported.
        -- /home/mosthated/_dev/languages/cyber/test_script.cy:17:20 runArgs:
        --         t = res.out.utf8()
        -- I also tried the following:

        print res.out
        print res.out.utf8()
        print string(res.out)
        print string(res.out.utf8())
MostHated commented 1 year ago

Sorry, of course I figured it out seconds after posting this.

It looks like the proper way to do this is:

func runArgs(arg):
    argStr = arg.utf8() -- Change the arg to utf8() from the start
    cmd = [ '/bin/bash', '-c', 'echo "{argStr}"' ]
    res = execCmd(cmd)

    -- ...
    print res.out -- Gives hello
fubark commented 1 year ago

Nice catch. Yea, I think we might just have os.args() be converted to utf8 since it's commonly used. Also, once we have type checking, you'll notice that you have a rawstring sooner (in case you forget to validate it), but string interpolation won't have type checking since it will do toString() for its parameters.

MostHated commented 1 year ago

It took me a little while to figure out how to try to verify what type an object was when creating and validating a map. Originally I was just trying to check if a file currently existed, if not, then I create it, but I didn't see any sort of exists() call anywhere for a file. (does this exist anywhere?)

I ended up doing the following:

data = {}
loaded = loadDataStore(dbPath, activeWin) 

print typeid(loaded)

-- After a few tests, I was able to determine that if loadDataStore 'failed', it returned typeid of 2, but if it came back properly, it was 11
-- I originally tried to use catch, as I saw in the documentation, but I could not get it working. I am sure I must have done something improperly.
-- Doing this ended up with the desired results in the mean-time.

if typeid(loaded) is 11 and loaded.size() != 0:
  data = { index: loaded.index, windowid: loaded.windowid }
else:
  data = { index: 0, windowid: activeWin }

-- ...

--| Load Data Store --------------
--|-------------------------------
func loadDataStore(db, winId):
    cyonFile = try os.openFile('{db}/{winId}', #read)
    print cyonFile

    strCyon = cyonFile.readToEnd().utf8()
    cyonFile.close()

    if strCyon == {} or strCyon == "":
      return {}
    else:
      objCyon = parseCyon(strCyon)
      return objCyon

From the sound of it, additional type checking is coming? That is good news.

All said and done, so far, I am really liking the language, minus a slight bit of frustration of things not quite working as expected, or at all, but I mean, its what, version 0.2? That is expected, so not holding that against it at all.

Due to the complexity of some of my scripts I use for automation on my pc, I have been using mostly PowerShell as the vast utility from objects and it's string manipulation ability leave bash in the dust in terms of what you can accomplish with mimimal code. The issue is the memory usage and startup time leaves something to be desired in the case of things you want to run frequently. Some of my scripts run every 1-2 seconds, others I have to run every 5 due to powershell/dotnet clr overhead. I can usually solve this using dotnet-script/*.csx files, but there is a lot more ceremony and boiler plate, etc for trying to get speed out of smaller scripts in a short amount of time.

From the looks of it, this language might be a solid additional tool under my belt for when speed is necessary. I think it has a great future, and I am excited to keep using it. :+1:

fubark commented 1 year ago

Thanks for the suggestion. I think we'll add os.access(path) to check for exists and other errors.

typeid() is a bit lower level and returns the underlying ids, strings may have different ids for example. For a higher level alternative, you can use valtag(loaded) == #map

You can also check for errors like this loaded == error(#FileNotFound) which can be returned from openFile.

MostHated commented 1 year ago

One last side note. I have been working on this today (far from complete, or even correct) but wanted to check to see if this is something you might be interested in bringing into the project once I completed it, or if I should make it a separate package?

Enhancements to the Vim plugin

https://github.com/instance-id/cyber/commit/6dd70cc1d8256c023c885786320971c040cf7735

It still has a ways to go, but it's getting there:

Thanks, -MH

fubark commented 1 year ago

That's great! Yea we can replace the stock version if it works. And if you want more control like creating install packages for it, you can also start another repo. Then we add a link here just like the vscode extension by Daelon.

MostHated commented 1 year ago

I actually ended up going a bit overboard.

https://github.com/instance-id/nvim-cyber https://github.com/instance-id/tree-sitter-cyber

It'a super basic, as I have never used treesitter before from a development aspect, but a solid framework is there, and it works. I figured that if I was going to put the time in, may as well make it worth it, lol. Will be much more worth it in the long run.

fubark commented 1 year ago

I might just have to switch to nvim now. This looks great!

MostHated commented 1 year ago

Now we are getting somewhere. :+1: It's starting to look respectable, at least. Treesitter isn't the easiest thing to jump into, but I am starting to get the hang of it.

Maybe soon I can actually get back to writing some scripts. lol.

fubark commented 1 year ago

I have added links to nvim/treesitter in the exts folder. Really excited for your latest adventure into LSP since I'll be able to use it from sublime!

I'm closing this ticket for cleanup since os.access was added and all the builtin functions are now typed so it makes it easier to discover problems sooner. valtag has also been renamed to typesym since there is no concept of tags anymore (They are referred to as symbols now)

MostHated commented 1 year ago

Yeah, no worries. I refreshed my local repo of cyber yesterday and noticed some of the changes. I am going to have to keep track of the changes more often so I can update appropriately on my end.

At some point, it might be a good idea to have a quick chat about the future plans for documentation. I can create a discussion page about that, though, or a discord thread or something.