diff --git a/src/main/resources/assets/computercraft/lua/bios.lua b/src/main/resources/assets/computercraft/lua/bios.lua index a06743eb2..7115538a5 100644 --- a/src/main/resources/assets/computercraft/lua/bios.lua +++ b/src/main/resources/assets/computercraft/lua/bios.lua @@ -6,6 +6,18 @@ if _VERSION == "Lua 5.1" then local nativeloadstring = loadstring local nativesetfenv = setfenv function load( x, name, mode, env ) + if type( x ) ~= "string" then + error( "bad argument #1 (expected string, got " .. type( x ) .. ")", 2 ) + end + if name ~= nil and type( name ) ~= "string" then + error( "bad argument #2 (expected string, got " .. type( name ) .. ")", 2 ) + end + if mode ~= nil and type( mode ) ~= "string" then + error( "bad argument #3 (expected string, got " .. type( mode ) .. ")", 2 ) + end + if env ~= nil and type( env) ~= "table" then + error( "bad argument #4 (expected table, got " .. type( env ) .. ")", 2 ) + end if mode ~= nil and mode ~= "t" then error( "Binary chunk loading prohibited", 2 ) end @@ -709,6 +721,9 @@ if http then if _headers ~= nil and type( _headers ) ~= "table" then error( "bad argument #2 (expected table, got " .. type( _headers ) .. ")", 2 ) end + if _binary ~= nil and type( _binary ) ~= "boolean" then + error( "bad argument #3 (expected boolean, got " .. type( _binary ) .. ")", 2 ) + end return wrapRequest( _url, nil, _headers, _binary) end @@ -722,6 +737,9 @@ if http then if _headers ~= nil and type( _headers ) ~= "table" then error( "bad argument #3 (expected table, got " .. type( _headers ) .. ")", 2 ) end + if _binary ~= nil and type( _binary ) ~= "boolean" then + error( "bad argument #4 (expected boolean, got " .. type( _binary ) .. ")", 2 ) + end return wrapRequest( _url, _post or "", _headers, _binary) end @@ -735,6 +753,9 @@ if http then if _headers ~= nil and type( _headers ) ~= "table" then error( "bad argument #3 (expected table, got " .. type( _headers ) .. ")", 2 ) end + if _binary ~= nil and type( _binary ) ~= "boolean" then + error( "bad argument #4 (expected boolean, got " .. type( _binary ) .. ")", 2 ) + end local ok, err = nativeHTTPRequest( _url, _post, _headers, _binary ) if not ok then os.queueEvent( "http_failure", _url, err ) diff --git a/src/main/resources/assets/computercraft/lua/rom/apis/io.lua b/src/main/resources/assets/computercraft/lua/rom/apis/io.lua index d93ef9fdf..71e792164 100644 --- a/src/main/resources/assets/computercraft/lua/rom/apis/io.lua +++ b/src/main/resources/assets/computercraft/lua/rom/apis/io.lua @@ -49,11 +49,14 @@ function input( _arg ) elseif _G.type( _arg ) == "nil" then return g_currentInput else - error( "Expected file name or file handle" ) + error( "bad argument #1 (expected string/table/nil, got " .. type( _arg ) .. ")", 2 ) end end function lines( _sFileName ) + if type( _sFileNamel ) ~= "string" then + error( "bad argument #1 (expected string, got " .. type( _sFileName ) .. ")", 2 ) + end if _sFileName then return open( _sFileName, "r" ):lines() else @@ -62,6 +65,12 @@ function lines( _sFileName ) end function open( _sPath, _sMode ) + if type( _sPath ) ~= "string" then + error( "bad argument #1 (expected string, got " .. type( _sPath ) .. ")", 2 ) + end + if type( _sMode ) ~= "string" then + error( "bad argument #2 (expected string, got " .. type( _sMode ) .. ")", 2 ) + end local sMode = _sMode or "r" local file, err = fs.open( _sPath, sMode ) if not file then @@ -158,7 +167,7 @@ function output( _arg ) elseif _G.type( _arg ) == "nil" then return g_currentOutput else - error( "Expected file name or file handle" ) + error( "bad argument #1 (expected string/table/nil, got " .. type( _arg ) .. ")", 2 ) end end diff --git a/src/main/resources/assets/computercraft/lua/rom/apis/rednet.lua b/src/main/resources/assets/computercraft/lua/rom/apis/rednet.lua index 1cc0c4ce2..9c5e2289e 100644 --- a/src/main/resources/assets/computercraft/lua/rom/apis/rednet.lua +++ b/src/main/resources/assets/computercraft/lua/rom/apis/rednet.lua @@ -8,7 +8,7 @@ local tHostnames = {} function open( sModem ) if type( sModem ) ~= "string" then - error( "expected string", 2 ) + error( "bad argument #1 (expected string, got " .. type( sModem ) .. ")", 2 ) end if peripheral.getType( sModem ) ~= "modem" then error( "No such modem: "..sModem, 2 ) @@ -21,7 +21,7 @@ function close( sModem ) if sModem then -- Close a specific modem if type( sModem ) ~= "string" then - error( "expected string", 2 ) + error( "bad argument #1 (expected string, got " .. type( sModem ) .. ")", 2 ) end if peripheral.getType( sModem ) ~= "modem" then error( "No such modem: "..sModem, 2 ) @@ -42,7 +42,7 @@ function isOpen( sModem ) if sModem then -- Check if a specific modem is open if type( sModem ) ~= "string" then - error( "expected string", 2 ) + error( "bad argument #1 (expected string, got " .. type( sModem ) .. ")", 2 ) end if peripheral.getType( sModem ) == "modem" then return peripheral.call( sModem, "isOpen", os.getComputerID() ) and peripheral.call( sModem, "isOpen", CHANNEL_BROADCAST ) @@ -59,6 +59,12 @@ function isOpen( sModem ) end function send( nRecipient, message, sProtocol ) + if type( nRecipient ) ~= "number" then + error( "bad argument #1 (expected number, got " .. type( nRecipient ) .. ")", 2 ) + end + if sProtocol ~= nil and type( sProtocol ) ~= "string" then + error( "bad argument #3 (expected string, got " .. type( sProtocol ) .. ")", 2 ) + end -- Generate a (probably) unique message ID -- We could do other things to guarantee uniqueness, but we really don't need to -- Store it to ensure we don't get our own messages back @@ -93,6 +99,9 @@ function send( nRecipient, message, sProtocol ) end function broadcast( message, sProtocol ) + if sProtocol ~= nil and type( sProtocol ) ~= "string" then + error( "bad argument #2 (expected string, got " .. type( sProtocol ) .. ")", 2 ) + end send( CHANNEL_BROADCAST, message, sProtocol ) end @@ -101,6 +110,12 @@ function receive( sProtocolFilter, nTimeout ) if type(sProtocolFilter) == "number" and nTimeout == nil then sProtocolFilter, nTimeout = nil, sProtocolFilter end + if sProtocolFilter ~= nil and type( sProtocolFilter ) ~= "string" then + error( "bad argument #1 (expected string, got " .. type( sProtocolFilter ) .. ")", 2 ) + end + if nTimeout ~= nil and type( nTimeoutl ) ~= "number" then + error( "bad argument #2 (expected number, got " .. type( nTimeout ) .. ")", 2 ) + end -- Start the timer local timer = nil @@ -131,8 +146,11 @@ function receive( sProtocolFilter, nTimeout ) end function host( sProtocol, sHostname ) - if type( sProtocol ) ~= "string" or type( sHostname ) ~= "string" then - error( "expected string, string", 2 ) + if type( sProtocol ) ~= "string" then + error( "bad argument #1 (expected string, got " .. type( sProtocol ) .. ")", 2 ) + end + if type( sHostname ) ~= "string" then + error( "bad argument #2 (expected string, got " .. type( sHostname ) .. ")", 2 ) end if sHostname == "localhost" then error( "Reserved hostname", 2 ) @@ -147,14 +165,17 @@ end function unhost( sProtocol ) if type( sProtocol ) ~= "string" then - error( "expected string", 2 ) + error( "bad argument #1 (expected string, got " .. type( sProtocol ) .. ")", 2 ) end tHostnames[ sProtocol ] = nil end function lookup( sProtocol, sHostname ) if type( sProtocol ) ~= "string" then - error( "expected string", 2 ) + error( "bad argument #1 (expected string, got " .. type( sProtocol ) .. ")", 2 ) + end + if sHostname ~= nil and type( sHostname ) ~= "string" then + error( "bad argument #2 (expected string, got " .. type( sHostname ) .. ")", 2 ) end -- Build list of host IDs