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

[SOLVED]Hooking term.write bugs out print ?.?

Started by Pinkishu, 21 September 2012 - 11:52 AM
Pinkishu #1
Posted 21 September 2012 - 01:52 PM
Hi,

so i'm trying to hook term.write



local orgFuncs = {}
local hookFuncs = {}
hookFuncs.term = {}


function hookFuncs.term.write(str)
  return orgFuncs.term.write("a"..str)
end

function hook(t)
  if t ~= nil then tb = hookFuncs[t] else tb = hookFuncs end
  for k,v in pairs(tb) do
   if type(v) == "table" then
	 hook(k)
   else
	 if t and t ~= "hookFuncs" then
	  if orgFuncs[t] == nil then orgFuncs[t] = {} end
	 -- term.write("REPL: "..t.."//"..k)
	  orgFuncs[t][k] = _G[t][k]
	   _G[t][k] = v
	 end
	  orgFuncs[k] = _G[k]
	   _G[k] = v
   end
  end
end

function unhook(t)
  if t ~= nil then tb = orgFuncs[t] else tb = orgFuncs end
  for k,v in pairs(tb) do
	if type(v) == "table" then
	 unhook(k)
	else
   if t and t ~= "orgFuncs" then
   _G[t][k] = v
   else
   _G[k] = v
   end
   end
  end
end
print("OK")
hook()
print("HOOKED")
unhook()
print("UNHOOKED")

However for some weird reason after i called hook() the print("HOOKED") prints "aHOOKED" but then errors:
bios:94: attempt to perform arithmetic __add on number and nil

Line 94 of bios.lua:

nLinesPrinted = nLinesPrinted + write( tostring( v ) )

So write returns a nil…

write func:

function write( sText )
  local nLinesPrinted = 0
  [...]
  nLinesPrinted = nLinesPrinted + 1
  [...]
  return nLinesPrinted
end

inserting a term.write before the return shows that it will return 1
however print now gets nil as the return of calling write… any ideas?
Fatal_Exception #2
Posted 21 September 2012 - 03:12 PM
Print is a wrapper around write.
Pinkishu #3
Posted 21 September 2012 - 03:14 PM
Yeah, so?

Well found my mistake <.<