pcAll

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

pcAll

Post by bartbes »

I know, I know, but wait until I explain the name:
pcall + All -> pcAll, got it?

Well, basically I've explained the entire purpose with that sentence, this is a tiny lib (if you can call it that) that can convert every table into one where every function is pcalled. So if you don't like big screaming (blue) errors, try this.

I'm open for suggestions, so if you want to have anything changed, ask me.

Further information is posted on request (because I don't want to write an entire story, just so everyone skips it).

Let me explain some more:
pcall is a standard lua function meaning: call in 'protected mode'.
Protected mode means that all errors are catched and processed silently instead of just stopping (in LÖVE with a Blue Screen Of Dying LÖVE).
pcAll is there so you can wrap tables (and now functions) so everytime you call them (or rather the new handle returned by pcAll) the function gets pcalled instead.
This means that every wrapped function then returns a boolean (true/false) saying if the function succeeded and the normal output or the error (as returned by lua).

Hope that's enough.
Attachments
pcAll.lua
28 February 2009
(2.75 KiB) Downloaded 278 times
Last edited by bartbes on Sat Feb 28, 2009 2:20 pm, edited 1 time in total.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: pcAll

Post by bartbes »

Sorry for the three people who had already downloaded it, I forgot an important part. (it didn't set the variables in the original when they were set in the new one)
I should check my code more thoroughly before releasing...
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: pcAll

Post by bartbes »

New version (yeah, I know...), this one is a bit better commented and supports functions AND tables instead of just tables.
Code example:

Code: Select all

mytable = {}
mytable.string = "Test variable"
function mytable.give_error()
    error("This always errors")
end

function myfunction()
    error("And this as well")
end

wtable = pcAll(mytable)
wfunction = pcAll(myfunction)

mytable.give_error()    --This will give the error, as expected
myfunction()            --This too

wtable.give_error()     --Returns false and the error, but continues
wfunction()             --Same

--Something else, this all works
print(wtable.string)    --Prints "Test variable"
wtable.string = "Changed variable"
--Variable was changed in the table returned by pcAll, this actually sets the value in the original table
print(mytable.string)   --Prints "Changed variable"
User avatar
TsT
Party member
Posts: 161
Joined: Thu Sep 25, 2008 7:04 pm
Location: France
Contact:

Re: pcAll

Post by TsT »

You current version work good with lua 5.1 but not with lua 5.0.

If you want to be compatible with the both change the call of "..." to "arg" :

Code: Select all

--- pcall/pcAll.lua     (original)
+++ pcall/pcAll.lua     (working copy)
@@ -43,7 +43,7 @@
 end

 function wrap:__call(...)              --The function where the original is pcalled
-       return pcall(self.f, ...)
+       return pcall(self.f, arg)
 end

 function pcAll_private_mt:__call(t)    --This is to make it possible to use the table pcAll as a function (example: wrap_table = pcAll(orig_table) )
My projects current projects : dragoon-framework (includes lua-newmodule, lua-provide, lovemodular, , classcommons2, and more ...)
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: pcAll

Post by bartbes »

Fair enough. But I do have to ask, why bother making it 5.0 compatible? New software should be written on 5.1, and this still is the LÖVE forums, if it works with LÖVE it's good?
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests