This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
corisco1917's profile picture

[Question] api.native

Started by corisco1917, 08 February 2013 - 03:47 PM
corisco1917 #1
Posted 08 February 2013 - 04:47 PM
i've been messing around with some native functions from computercraft and some programans in these forum…

i found something very odd

native = term.native or term

native = turtle.native or turtle


what this . native means? can someone explain? i've tried to look in the wiki but there is no info about it.

i know its a table with k,v about the function… but what the method nativ is for, because it could be stored on _G
tesla1889 #2
Posted 08 February 2013 - 04:51 PM
native is a variable that contains what im guessing is the set of functions that write to the screen

EDIT: term.native isnt taken away
why not just examine it with a simple program?


local native = term.native
print("native analysis:")
local tn = type(native)
print("type: " .. tn)
print("tostring: " .. tostring(native))
if (tn == "table") then
print("table contents:")
for k,v in pairs(native) do
print(k .. " : " .. v)
end
elseif (tn == "function") then
print("calling function:")
native()
end
theoriginalbit #3
Posted 08 February 2013 - 04:54 PM
you can also use the native term if you wish to overwrite some term stuff

This is incorrect and would actually cause and error


term.clear = function(c) term.setBackgroundColor(c or colors.white) term.clear() end
The reason it would error is that you are calling your own function again by using just term.clear()

This is correct

term.clear = function(c) term.setBackgroundColor(c or colors.white) term.native.clear() end
tesla1889 #4
Posted 08 February 2013 - 04:58 PM
–snip–
ah. that makes a lot of sense.
theoriginalbit #5
Posted 08 February 2013 - 05:02 PM
ah. that makes a lot of sense.
Yep :)/>

Also noteworthy if your overwriting the entire term table (for whatever reason, maybe sandboxing something?) like this
term = {}

You would actually need to do it like this
term = {native=term.native}
that way all term functions are removed but you still have the native to actually do stuff with…
tesla1889 #6
Posted 08 February 2013 - 06:19 PM
–snip–

usually sandboxing involves creating a custom environment table, then entering using setfenv

anyone doing it the hard way is just crazy
corisco1917 #7
Posted 08 February 2013 - 06:35 PM
very good original, i understand now… but where i can find the native functions… is it possible to modificate them?
tesla1889 #8
Posted 08 February 2013 - 06:36 PM
you can, but it isnt wise

you can find the native functions in term.native
corisco1917 #9
Posted 08 February 2013 - 06:37 PM
i know it isn't its just for study.. do you know where native files are located?

i guess native functions are stored in those dan files right?
tesla1889 #10
Posted 08 February 2013 - 06:40 PM
oh, you want to know where the native definitions are?

decompile ComputerCraft and read the java source code

with enough patience, you can follow the code back from the initializer of the Computer class to the definitions of the native functions
corisco1917 #11
Posted 08 February 2013 - 06:46 PM
thanks for the tip tesla.
theoriginalbit #12
Posted 08 February 2013 - 06:46 PM
usually sandboxing involves creating a custom environment table, then entering using setfenv

anyone doing it the hard way is just crazy
True but doesn't mean someone isn't going to do it…
GopherAtl #13
Posted 08 February 2013 - 07:13 PM
overriding most term functions this way is a bad idea, it will cause the functions to stop working when you use term.redirect(). Making a redirect object to pass to term.redirect is the prefered method in most cases; where that isn't enough, you need to save the old term.whatever method to call from yours, rather than calling term.native.whatever. term.native functions always affect the actual computer term, ignoring redirects.
ChunLing #14
Posted 08 February 2013 - 07:20 PM
To answer the original question a bit,

The purpose of the above code snippet in a Lua API wrapper for an API provided by the Java is to put the native Java functions into a subtable (under the identifier native).

The functionality of or means that "term.native or term" will evaluate to term.native unless it is false or nil, in which case it will evaluate to term. That is a quick was of making sure that the native functions are backed up if the native subtable doesn't exist yet, and that it isn't overwritten with the (already modified) functions if it does already exist (the native functions wouldn't be lost, but they'd be under term.native.native if you were to just set native = term without checking for the native subtable).

Now, to understand what the native functions do, they mostly just do what the native functions do…this code here is copying all functions in the native table into term, except for those that are already in term.
for k,v in pairs( native ) do
    if type( k ) == "string" and type( v ) == "function" then
        if term[k] == nil then
            term[k] = wrap( k )
        end
    end
end
The exceptions are redirect and restore, which are defined so as to allow term functions to be redirected or restored, using the original functions in native as the base and a stack to keep track of the redirects. As mentioned, redirect exists to prevent you from having to fiddle with the functions too much, restore makes it easy to undo redirect.