Jump to content




load() with large code


  • You cannot reply to this topic
7 replies to this topic

#1 Wilma456

  • Members
  • 187 posts
  • LocationGermany

Posted 18 November 2016 - 06:32 PM

load() does not work with large code. For example:
file = fs.open("/rom/programs/edit","r")
load(file.readAll())("example")
This give me a string error. The same problem with other programs. Have anybody a solution for this? In the programm who I'm need it, the code is just a string and not are file like the example.

#2 TheRockettek

  • Members
  • 547 posts
  • LocationRem is best girl

Posted 18 November 2016 - 06:34 PM

you can do loadfile(path) i believe...

#3 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 18 November 2016 - 07:31 PM

Maybe the code is malformed in some way? It should give you more than just a "string error".

Try using load instead of loadstring

#4 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

Posted 19 November 2016 - 12:17 AM

View PostH4X0RZ, on 18 November 2016 - 07:31 PM, said:

Maybe the code is malformed in some way? It should give you more than just a "string error".

Try using load instead of loadstring
He did.

I think the problem is the environment.
Shell is using a special environment to run programs with the shell and multishell API.
For some reasons, the shell API isn't global, so you have to add the API into the environment:

load(sFile, 'edit', setmetatable({shell = shell, multishell = multishell}, {__index = _G}))
Note, you need this code just if you wanna use the shell API.

Edited by Sewbacca, 19 November 2016 - 12:18 AM.


#5 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 19 November 2016 - 02:15 AM

I know nothing about load, but in the code given by OP there are two discreet sets of brackets on the load line - is that even valid for load? To me it appears that he's attempting to load file.readAll() then he has "example" enclosed in a separate set of brackets.

#6 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 19 November 2016 - 02:56 AM

<p>

View PostDog, on 19 November 2016 - 02:15 AM, said:

I know nothing about load, but in the code given by OP there are two discreet sets of brackets on the load line - is that even valid for load?  To me it appears that he's attempting to load file.readAll() then he has "example" enclosed in a separate set of brackets.

Load returns a function (which is the string loaded as code) which you either save in some variable or execute directly.
local function foo() return function(bar)  print(bar) end end foo()("bar") --> prints bar
Damn mobile Firefox. Messes up the code...

Edited by H4X0RZ, 19 November 2016 - 02:58 AM.


#7 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 19 November 2016 - 03:12 AM

Oh, wow - I didn't know that was possible - thanks, H4X0RZ, I learned something new today :)

#8 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 19 November 2016 - 11:15 AM

<p>

View PostDog, on 19 November 2016 - 03:12 AM, said:

Oh, wow - I didn't know that was possible - thanks, H4X0RZ, I learned something new today :)
Btw, this allows you to do some funny stuff :D
I "like" "pancakes"
is (theoretically) valid syntax: it's a function taking one string as parameter, returning another function with yet another string as parameter.

Edited by H4X0RZ, 19 November 2016 - 11:15 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users