Limited dostring()?

classic Classic list List threaded Threaded
17 messages Options
Reply | Threaded
Open this post in threaded view
|

Limited dostring()?

Markus Huber
Hi Lua Fans,

   why doesn't work the following example?

   Name='Test'
   function Test_Version() return 001 end
   print(dostring(Name..'_Version()'))

    Prints: userdata(0): 00000000
But I need: 1


Markus


Reply | Threaded
Open this post in threaded view
|

RE: Limited dostring()?

Danilo Tuler de Oliveira

"If there is any error executing the string, then dostring returns nil. 
Otherwise, it returns the values returned by the chunk, or a non-nil 
value if the chunk returns no values" (userdata(0) actually).

if you do:
print(dostring('return '..Name..'_Version()'))

you get what you desire.

-- Danilo


-----Original Message-----
From: [hidden email]
[[hidden email] Behalf Of Markus Huber
Sent: quarta-feira, 9 de janeiro de 2002 00:14
To: Multiple recipients of list
Subject: Limited dostring()?


> Hi Lua Fans,

>    why doesn't work the following example?
>    Name='Test'
>    function Test_Version() return 001 end
>    print(dostring(Name..'_Version()'))

>     Prints: userdata(0): 00000000
> But I need: 1
> Markus





Reply | Threaded
Open this post in threaded view
|

Re: Limited dostring()?

Eric Tetz-2
In reply to this post by Markus Huber
--- Markus Huber <[hidden email]> wrote:
> Hi Lua Fans,
> 
>    why doesn't work the following example?
> 
>    Name='Test'
>    function Test_Version() return 001 end
>    print(dostring('return '..Name..'_Version()'))
> 
>     Prints: userdata(0): 00000000
> But I need: 1

Not the change to the print(dostring(... line.

Cheers,
Eric

__________________________________________________
Do You Yahoo!?
Send FREE video emails in Yahoo! Mail!
http://promo.yahoo.com/videomail/

Reply | Threaded
Open this post in threaded view
|

Re: Limited dostring()?

Thatcher Ulrich
In reply to this post by Markus Huber
On Jan 09, 2002 at 01:14 +0100, Markus Huber wrote:
> Hi Lua Fans,
> 
>    why doesn't work the following example?
> 
>    Name='Test'
>    function Test_Version() return 001 end
>    print(dostring(Name..'_Version()'))
> 
>     Prints: userdata(0): 00000000
> But I need: 1

Try:

print(dostring('return '..Name.."_Version()"))

-- 
Thatcher Ulrich <[hidden email]>
http://tulrich.com

Reply | Threaded
Open this post in threaded view
|

Re: Limited dostring()?

Steve Dekorte-4
In reply to this post by Markus Huber

On Tuesday, January 8, 2002, at 04:14  PM, Markus Huber wrote:

   why doesn't work the following example?

   Name='Test'
   function Test_Version() return 001 end
   print(dostring(Name..'_Version()'))

    Prints: userdata(0): 00000000
But I need: 1

try:

  print(dostring('return '..Name..'_Version()'))

Steve



Reply | Threaded
Open this post in threaded view
|

Re: Limited dostring()?

Markus Huber
In reply to this post by Markus Huber
Thanks for the answers.

> Danilo wrote:
> 
> "If there is any error executing the string, then dostring returns nil.
> Otherwise, it returns the values returned by the chunk, or a non-nil 
> value if the chunk returns no values" (userdata(0) actually).

   function Test_Version() return 001 end

      Name = 'Test'
   Version = 002

   if dostring('return '..Name..'_Version()')<Version then
      print('Version to old')
   end


If there is an error executing this dostring, e.g. if Name = 'Other'
then dostring shows an error message:
"error: attempt to call global 'Other_Version' (a nil value)"
and then
"error: attempt to compare nil with number"
How can both suppressed?


Please note: this is only the example extract from my environment. The
real problem is that I would like to write a few library management
functions. So the function names and version numbers are given in
the parameter list:

   Library_Initialise('Example',001)

   1. If 'Example' library isn't loaded the load it
      If 'Example' library not available then show an error message
   2. Check version of 'Example' library
      If 'Example' library to old then show an error message
   3. If 'Example' library isn't initialised then initialise it

Thank you very much for your help. If I missed to read some
documentation please let me know.


Markus


Reply | Threaded
Open this post in threaded view
|

Re: Limited dostring()?

Luiz Henrique de Figueiredo
In reply to this post by Markus Huber
>   if dostring('return '..Name..'_Version()')<Version then
>      print('Version to old')
>   end
>
>If there is an error executing this dostring, e.g. if Name = 'Other'
>then dostring shows an error message:
>"error: attempt to call global 'Other_Version' (a nil value)"
>and then
>"error: attempt to compare nil with number"
>How can both suppressed?

How about this (untested)?

	function getversion(Name)
	 local error=_ERRORMESSAGE
	 _ERRORMESSAGE=nil
	 local version=dostring('return '..Name..'_Version()') or 0
	 _ERRORMESSAGE=error
	 return version
	end

Setting _ERRORMESSAGE temporarily to nil prevents error messages.
Also, note "or 0"after dostring, that is, getversion returns 0 in case of
errors.
--lhf

Reply | Threaded
Open this post in threaded view
|

unsubscribe

Mario Udina
unsubscribe


Reply | Threaded
Open this post in threaded view
|

Re: Limited dostring()?

Roberto Ierusalimschy-5
In reply to this post by Markus Huber
On Wed, 9 Jan 2002, Markus Huber wrote:

>    why doesn't work the following example?
>
>    Name='Test'
>    function Test_Version() return 001 end
>    print(dostring(Name..'_Version()'))
>
>     Prints: userdata(0): 00000000
> But I need: 1

As a rule, you should always avoid using dostring if you can (it is too
expensive). A better way to do what you want is with getglobal:

  print(getglobal(Name..'_Version')())

Or, if you want to check for errors,

  local f = getglobal(Name..'_Version')
  if type(f) == 'function' then print(f())
  else ...
  end

-- Roberto


Reply | Threaded
Open this post in threaded view
|

Re: Limited dostring()?

Thomas Wrensch-2
Roberto wrote:
> As a rule, you should always avoid using dostring if you can (it is too
> expensive). A better way to do what you want is with getglobal:


Yes, it is more expensive. On my machine (W2K 750MHz PIII) this code:

    for i=1,100000 do
        local f = getglobal("Test_Version")
        f()
    end

Executed in 0.12 seconds, while this code:


    for i=1,100000 do
        dostring("return Test_Version()")
    end

Executed in 1.262 seconds.

So about a factor of 10 slower. 

However, I have to say that 12.62 microseconds to execute the
"dostring" version is pretty darn fast, plenty fast enough for many
situations.

  - Tom Wrensch
 


Reply | Threaded
Open this post in threaded view
|

Re: Limited dostring()?

Markus Huber
In reply to this post by Markus Huber
> Roberto wrote:

> As a rule, you should always avoid using dostring if you can (it is
> too expensive).

Is dofile() also so expensive? But I see no other way to load
code from external files (require() is not implemented in the version
4.0 and 4.1 is in beta state). Is require() faster as dofile()? What is
the difference between both? Should I write a pseudo require() and use
this until the 4.1 is ready to use?

Again thank you very much for your help. There are many discussions
about Lua and Robertos Lua book is fantastic but often its not easy to
find the definitive answers.

What methods are you suggests for variables that only hold an flag?
Which flag values? true/false nil/not nil...?


--
Markus


Reply | Threaded
Open this post in threaded view
|

Re: Limited dostring()?

Terence Martin-3
I may be wrong, but I think what Roberto was saying was, dostring is an
expensive operation because it has to compile the string before it can execute
it. Thus it's better for you to just query the global with one of the available
functions, rather than doing it in a way that has to compile the function every
time you do it. In that regard, dofile would also be expensive, unless you were
dofile'ing code that was precompiled with luac or something. Even then, you have
the overhead of loading in the code before it gets run.

As for require, I may not be 100%, but I belive it does the same thing as
dofile, except that it has a path that it will look for files in, and it won't
bother doing the same file a second time. So whereas

dofile ("bob.lua") dofile ("bob.lua")

will load, parse, and execute the file bob.lua twice,

require ("bob.lua") require ("bob.lua")

will only do it once. [1]

I would suspect that it is a bit slower than a regular dofile because it's doing
extra checks to see if it should even bother doing a file. I doubt the extra
penalty is large enough to be of any concern though. Subsequent calls with the
same filename would be faster, however, since once it determined that the file
was already loaded, it wouldn't do anything.

As for, should you write your own? If that's the sort of functionality you need,
it might be a good idea. Probably you could get away with "require = dofile" in
a pinch,and remove that when you upgrade.

And for storing a flag, you probably just want to test for nil/non-nil using a
number

[1]

I'm not entirely sure if require takes only one parameter or more than one. I
haven't actually used the new version yet. But I think it's like a "drop in
replacement" for dofile. 
======================
On Thu, 10 Jan 2002 21:57:09 +0100 (GMT)
Markus Huber <[hidden email]> wrote:

> > Roberto wrote:
> 
> > As a rule, you should always avoid using dostring if you can (it is
> > too expensive).
> 
> Is dofile() also so expensive? But I see no other way to load
> code from external files (require() is not implemented in the version
> 4.0 and 4.1 is in beta state). Is require() faster as dofile()? What is
> the difference between both? Should I write a pseudo require() and use
> this until the 4.1 is ready to use?
> 
> Again thank you very much for your help. There are many discussions
> about Lua and Robertos Lua book is fantastic but often its not easy to
> find the definitive answers.
> 
> What methods are you suggests for variables that only hold an flag?
> Which flag values? true/false nil/not nil...?
> 
> 
> --
> Markus
> 

Reply | Threaded
Open this post in threaded view
|

Re: Limited dostring()?

Luiz Henrique de Figueiredo
In reply to this post by Markus Huber
>Should I write a pseudo require() and use this until the 4.1 is ready to use?

Here is a version of require that works in 4.0:

	do
	 local A={}
	 function require(x)
	  if %A[x]==nil then %A[x]=1; return %dofile(x) end
	 end
	end

>What methods are you suggests for variables that only hold an flag?

	"true" -> 1,  "false" -> nil

--lhf

Reply | Threaded
Open this post in threaded view
|

Re: Limited dostring()?

Jay Carlson
In reply to this post by Terence Martin-3
> As for, should you write your own? If that's the sort of functionality you
need,
> it might be a good idea. Probably you could get away with "require =
dofile" in
> a pinch,and remove that when you upgrade.

In 4.0 I've been writing:

local require = require or dofile

but I guess that isn't future-proof....

Jay


Reply | Threaded
Open this post in threaded view
|

Re: Limited dostring()?

Edgar Toernig
In reply to this post by Luiz Henrique de Figueiredo
Luiz Henrique de Figueiredo wrote:
> 
> Here is a version of require that works in 4.0:
> 
>         do
>          local A={}
>          function require(x)
>           if %A[x]==nil then %A[x]=1; return %dofile(x) end
>          end
>         end

Btw, what do you think about a slight modification that detects
dependency loops?  (I do that in Sol.)

  if A[x]==nil then
    A[x]=1
    ...dofile...
    A[x]=2
    return results...
  elseif A[x]==1 then
    error("dependency loop")
  end

Ciao, ET.

Reply | Threaded
Open this post in threaded view
|

Re: Limited dostring()?

Philippe Lhoste
In reply to this post by Markus Huber
Edgar Toernig wrote:
> Btw, what do you think about a slight modification that detects
> dependency loops? (I do that in Sol.)

Out of curiosity:

Is Sol just a proof of concept or are you using it in your projects in place
of Lua?

Is Sol still evolving (in same or divergeant direction than Lua) or frozen
in the state your released it?

Regards.

-- 
--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--
Philippe Lhoste (Paris -- France)
Professional programmer and amateur artist
http://jove.prohosting.com/~philho/
--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--

Sent through GMX FreeMail - http://www.gmx.net


Reply | Threaded
Open this post in threaded view
|

Re: Limited dostring()?

Edgar Toernig
Philippe Lhoste wrote:
>
> Is Sol just a proof of concept or are you using it in your projects in place
> of Lua?

I'm using it instead of Lua.  But I still have no "bigger" application.  Only
some ad hock script's here and there...

> Is Sol still evolving (in same or divergeant direction than Lua) or frozen
> in the state your released it?

I didn't release it yet ;-)  What you see are only snapshots of the version
I'm currently using.  The "wishlist" becomes shorter now and maybe it's time
to write some documentation for it...

About differences to Lua.  I think with the last Lua snapshot both of them
become more similar again.  It's not may goal to make a "different" language.
I like Lua and try to keep Sol as similar to Lua as possible.  But I have
other priorities and there are some things I see different so I'll stay
working on Sol.  Pace is and will be slow though.  It's just a "hobby" project
I work on when I have some time to waste ;-)

Ciao, ET.