[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Simulating Lua states in Lua with only 250 lines of code
- From: Sean Conner <sean@...>
- Date: Sat, 27 Jun 2015 00:00:49 -0400
It was thus said that the Great Soni L. once stated:
>
>
> On 26/06/15 07:27 PM, Sean Conner wrote:
> >It was thus said that the Great Soni L. once stated:
> >>Lua States (from C) work well for sandboxing. I don't see why we
> >>SHOULDN'T have them available as coroutines. I believe the debug API
> >>should make Lua side just as powerful as C side (at least when it comes
> >>to VM interaction).
> > Then wrap Lua up into a module:
> >
> > lua = require "lua"
> >
> > L = lua.Lnewstate()
> > L:gc('stop')
> > L:Lopenlibs()
> > L:gc('start')
> But that looks nothing like Lua!
>
> lua = require "lua"
>
> L = lua.newstate() -- Luaism: optionally pass extra arguments for
> allocator/etc
> L:gc('stop')
> L:Lopenlibs() -- whatever
> L:gc('start')
> local r1, r2, r3 = L:pcall(L._G.f, ...) -- Luaism: varargs. and multiret.
I was going for a straight Lua translation of the C API (which is what a
lot of people appear to do instead of a more Luaish approach when wrapping a
C API) to get the point across clearly. There are two C calls to create a
Lua state:
lua_newstate()
luaL_newstate()
The first requires an allocation function, the second uses a built-in
allocator based n the standard C library functions. Certainly, a Luaish
version would use luaL_newstate() if nothing was passed in, but like I said,
this was demonstrative, not definitive.
> > ...
> >
> > -spc (Or instead of the entirety of Lua, just enough to sandbox like you
> > can in C ... )
> >
> Lua is a programming language. You should be able to use it to its
> fullest extent from itself. You don't need to use assembly to be able to
> use all of C's features: you can use all of C's features from C itself.
Such as? I know there are features of assembly language I can't access
from C (trapping on signed overflow on MIPS or the VAX, the carry bit from
any number of architectures, multiple return values---God I miss that in C).
For years, I considered C as a list of what you *can't* do when compared to
assembly (easily swap stacks, assume 2s compliment arithmetic, return
multiple results, call functions (or procedures, or subroutines) written in
*any* language, trivial to write bignum support due to access to the carry
flag, etc. etc.).
Also, there are programming languages where you can't use all the
features. Yes, you can write your own version of printf() in C, but you
can't write your own version println() in Pascal. Or your own version of
PRINT in Fortran. This isn't meant as an excuse for why Lua is unable to
use itself to the full extent, but to show that not all programming
languages allow that.
And please, tell me what feature of C allows me to compile and run C code
at run time?
> Same goes for Java (altho Scala is more powerful), and many other
> languages.
C is not reflective. Not to the degree that Java is.
> Why should it be different with Lua?
>
> My change also doesn't break any existing code. It also requires you to
> use the debug library. (Java's reflection = Lua's debug... Well actually
> Java's reflection > Lua's debug, beause Java's reflection lets you do
> much more than Lua's debug.)
And Lua's debug > than C's debug.
-spc (And Ruby is amateur when it comes to monkey patching when compared
to Forth, where you can redefine 2 to be a random value every time
its used ... )
- References:
- Simulating Lua states in Lua with only 250 lines of code, Soni L.
- Re: Simulating Lua states in Lua with only 250 lines of code, Daurnimator
- Re: Simulating Lua states in Lua with only 250 lines of code, Tim Hill
- Re: Simulating Lua states in Lua with only 250 lines of code, Soni L.
- Re: Simulating Lua states in Lua with only 250 lines of code, Tim Hill
- Re: Simulating Lua states in Lua with only 250 lines of code, Soni L.
- Re: Simulating Lua states in Lua with only 250 lines of code, Tim Hill
- Re: Simulating Lua states in Lua with only 250 lines of code, Soni L.
- Re: Simulating Lua states in Lua with only 250 lines of code, Sean Conner
- Re: Simulating Lua states in Lua with only 250 lines of code, Soni L.