changeset 539:473e456444ff

Remove object-oriented primitive methods for string and binary
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 01 Jun 2015 17:53:55 -0600
parents 919b9410008e
children 4362eb720da9
files core/src/luan/LuanTable.java core/src/luan/modules/BinaryLuan.java core/src/luan/modules/Io.luan core/src/luan/modules/String.luan core/src/luan/modules/StringLuan.java http/src/luan/modules/http/Http_test.luan http/src/luan/modules/http/Server.luan lucene/src/luan/modules/lucene/Ab_testing.luan lucene/src/luan/modules/lucene/Web_search.luan scripts/test.luan
diffstat 10 files changed, 39 insertions(+), 72 deletions(-) [+]
line wrap: on
line diff
diff -r 919b9410008e -r 473e456444ff core/src/luan/LuanTable.java
--- a/core/src/luan/LuanTable.java	Mon Jun 01 00:51:44 2015 -0600
+++ b/core/src/luan/LuanTable.java	Mon Jun 01 17:53:55 2015 -0600
@@ -132,10 +132,6 @@
 			LuanTable tbl = (LuanTable)obj;
 			return tbl.get(luan,key);
 		}
-		if( obj instanceof String )
-			return StringLuan.__index(bit,(String)obj,key);
-		if( obj instanceof byte[] )
-			return BinaryLuan.__index(bit,(byte[])obj,key);
 		if( obj != null && luan.hasJava() )
 			return JavaLuan.__index(bit,obj,key,false);
 		else if( bit.el==null )
@@ -155,6 +151,10 @@
 		}
 		if( map==null )
 			return null;
+		if( key instanceof Number && !(key instanceof Double) ) {
+			Number n = (Number)key;
+			key = Double.valueOf(n.doubleValue());
+		}
 		return map.get(key);
 	}
 
@@ -202,9 +202,8 @@
 				}
 			}
 		}
-		if( map==null ) {
+		if( map==null )
 			map = newMap();
-		}
 		if( key instanceof Number && !(key instanceof Double) ) {
 			Number n = (Number)key;
 			key = Double.valueOf(n.doubleValue());
diff -r 919b9410008e -r 473e456444ff core/src/luan/modules/BinaryLuan.java
--- a/core/src/luan/modules/BinaryLuan.java	Mon Jun 01 00:51:44 2015 -0600
+++ b/core/src/luan/modules/BinaryLuan.java	Mon Jun 01 17:53:55 2015 -0600
@@ -11,27 +11,6 @@
 
 public final class BinaryLuan {
 
-	public static Object __index(LuanBit bit,final byte[] binary,Object key) throws LuanException {
-		LuanState luan = bit.luan;
-		LuanTable mod = (LuanTable)PackageLuan.require(luan,"luan:Binary");
-		Object obj = mod.get(luan,key);
-		if( obj instanceof LuanFunction ) {
-			final LuanFunction fn = (LuanFunction)obj;
-			return new LuanFunction() {
-				@Override public Object call(LuanState luan,Object[] args) throws LuanException {
-					Object[] a = new Object[args.length+1];
-					a[0] = binary;
-					System.arraycopy(args,0,a,1,args.length);
-					return fn.call(luan,a);
-				}
-			};
-		}
-		if( bit.el != null )
-			throw bit.exception( "invalid index for binary in '"+bit.el.text()+"'" ) ;
-		else
-			throw bit.exception( "invalid index for binary") ;
-	}
-
 	static int start(byte[] binary,int i) {
 		int len = binary.length;
 		return i==0 ? 0 : i > 0 ? Math.min(i-1,len) : Math.max(len+i,0);
diff -r 919b9410008e -r 473e456444ff core/src/luan/modules/Io.luan
--- a/core/src/luan/modules/Io.luan	Mon Jun 01 00:51:44 2015 -0600
+++ b/core/src/luan/modules/Io.luan	Mon Jun 01 17:53:55 2015 -0600
@@ -19,11 +19,17 @@
 local try = Luan.try
 local ipairs = Luan.ipairs
 local pairs = Luan.pairs
+local values = Luan.values
 local Table = require "luan:Table"
+local unpack = Table.unpack
+local String = require "luan:String"
+local encode = String.encode
+local matches = String.matches
+
 
 function M.print_to(out,...)
 	local list = {}
-	for v in Luan.values(...) do
+	for v in values(...) do
 		list[#list+1] = to_string(v)
 		list[#list+1] = '\t'
 	end
@@ -31,7 +37,7 @@
 		out.write( '\n' )
 	else
 		list[#list] = '\n'
-		out.write( Table.unpack(list) )
+		out.write( unpack(list) )
 	end
 end
 
@@ -61,7 +67,7 @@
 		for key, value in pairs(obj) do
 			if in_list[key] ~= true then
 				if is_first then is_first = false else %>, <% end
-				if type(key) == "string" and key.match "^[a-zA-Z_][a-zA-Z_0-9]*$" ~= nil then
+				if type(key) == "string" and matches(key,"[a-zA-Z_][a-zA-Z_0-9]*") ~= nil then
 					%><%=key%><%
 				elseif type(key) == "table" then
 					%>[<<%=key%>>]<%
@@ -73,7 +79,7 @@
 		end
 		%>}<%
 	elseif tp == "string" then
-		%>"<%=obj.encode()%>"<%
+		%>"<%=encode(obj)%>"<%
 	elseif tp == "nil" or tp == "boolean" or tp == "number" then
 		%><%=obj%><%
 	else
@@ -107,7 +113,7 @@
 
 function M.dont_write_when_no(write_fn)
 	return function(...)
-		for v in Luan.values(...) do
+		for v in values(...) do
 			if v == NO then
 				return
 			end
diff -r 919b9410008e -r 473e456444ff core/src/luan/modules/String.luan
--- a/core/src/luan/modules/String.luan	Mon Jun 01 00:51:44 2015 -0600
+++ b/core/src/luan/modules/String.luan	Mon Jun 01 17:53:55 2015 -0600
@@ -19,9 +19,9 @@
 M.matches = StringLuan.matches
 M.rep = StringLuan.rep
 M.reverse = StringLuan.reverse
+M.string_to_binary = StringLuan.string_to_binary
+M.string_to_number = StringLuan.string_to_number
 M.sub = StringLuan.sub
-M.to_binary = StringLuan.to_binary
-M.to_number = StringLuan.to_number
 M.trim = StringLuan.trim
 M.upper = StringLuan.upper
 
diff -r 919b9410008e -r 473e456444ff core/src/luan/modules/StringLuan.java
--- a/core/src/luan/modules/StringLuan.java	Mon Jun 01 00:51:44 2015 -0600
+++ b/core/src/luan/modules/StringLuan.java	Mon Jun 01 17:53:55 2015 -0600
@@ -15,32 +15,6 @@
 
 public final class StringLuan {
 
-	public static Object __index(LuanBit bit,final String s,Object key) throws LuanException {
-		LuanState luan = bit.luan;
-		LuanTable mod = (LuanTable)PackageLuan.require(luan,"luan:String");
-		Object obj = mod.get(luan,key);
-		if( obj instanceof LuanFunction ) {
-			final LuanFunction fn = (LuanFunction)obj;
-			return new LuanFunction() {
-				@Override public Object call(LuanState luan,Object[] args) throws LuanException {
-					Object[] a = new Object[args.length+1];
-					a[0] = s;
-					System.arraycopy(args,0,a,1,args.length);
-					return fn.call(luan,a);
-				}
-			};
-		}
-		if( luan.hasJava() ) {
-			Object rtn = JavaLuan.__index(bit,s,key,true);
-			if( rtn != JavaLuan.FAIL )
-				return rtn;
-		}
-		if( bit.el != null )
-			throw bit.exception( "invalid index for string in '"+bit.el.text()+"'" ) ;
-		else
-			throw bit.exception( "invalid index for string") ;
-	}
-
 	static int start(String s,int i) {
 		int len = s.length();
 		return i==0 ? 0 : i > 0 ? Math.min(i-1,len) : Math.max(len+i,0);
@@ -78,7 +52,7 @@
 		return new String(a);
 	}
 
-	@LuanMethod public static byte[] to_binary(String s) {
+	@LuanMethod public static byte[] string_to_binary(String s) {
 		return s.getBytes();
 	}
 
@@ -249,7 +223,7 @@
 		return Luan.stringEncode(s);
 	}
 
-	public static Number to_number(LuanState luan,String s,Integer base) throws LuanException {
+	public static Number string_to_number(LuanState luan,String s,Integer base) throws LuanException {
 		Utils.checkNotNull(luan,s);
 		try {
 			if( base == null ) {
diff -r 919b9410008e -r 473e456444ff http/src/luan/modules/http/Http_test.luan
--- a/http/src/luan/modules/http/Http_test.luan	Mon Jun 01 00:51:44 2015 -0600
+++ b/http/src/luan/modules/http/Http_test.luan	Mon Jun 01 17:53:55 2015 -0600
@@ -1,4 +1,6 @@
 local Io = require "luan:Io"
+local String = require "luan:String"
+local matches = String.matches
 local Http = require "luan:http/Http"
 
 local M = {}
@@ -7,7 +9,7 @@
 M.cookie = {}
 
 function M.get_page(path)
-	if M.welcome_file ~= nil and path.matches ".*/" then
+	if M.welcome_file ~= nil and matches(path,".*/") then
 		path = path .. M.welcome_file
 	end
 	local old_out = Io.stdout
diff -r 919b9410008e -r 473e456444ff http/src/luan/modules/http/Server.luan
--- a/http/src/luan/modules/http/Server.luan	Mon Jun 01 00:51:44 2015 -0600
+++ b/http/src/luan/modules/http/Server.luan	Mon Jun 01 17:53:55 2015 -0600
@@ -1,4 +1,6 @@
-require "luan:String"
+local String = require "luan:String"
+local gsub = String.gsub
+local match = String.match
 local Io = require "luan:Io"
 local Package = require "luan:Package"
 local Http = require "luan:http/Http"
@@ -72,14 +74,14 @@
 
 
 function M.init(dir)
-	dir = dir.gsub("/$","")  -- remove trailing '/' if any
+	dir = gsub(dir,"/$","")  -- remove trailing '/' if any
 	Http.dir = dir
 	function Io.schemes.site(path,add_extension)
 		return Io.uri( dir..path, add_extension )
 	end
 	M.authentication_handler.setPassword(M.private_password)
 	local base = dir
-	if base.match("^classpath:") ~= nil then
+	if match(base,"^classpath:") ~= nil then
 		base = dir.."#"..M.welcome_file.."#"..M.welcome_file..".luan"
 	end
 	M.resource_handler.setResourceBase(Io.uri(base).to_string())
diff -r 919b9410008e -r 473e456444ff lucene/src/luan/modules/lucene/Ab_testing.luan
--- a/lucene/src/luan/modules/lucene/Ab_testing.luan	Mon Jun 01 00:51:44 2015 -0600
+++ b/lucene/src/luan/modules/lucene/Ab_testing.luan	Mon Jun 01 17:53:55 2015 -0600
@@ -5,6 +5,8 @@
 local range = Luan.range
 local Math = require "luan:Math"
 local Table = require "luan:Table"
+local String = require "luan:String"
+local gsub = String.gsub
 local Io = require "luan:Io"
 local Http = require "luan:http/Http"
 local Logging = require "luan:logging/Logging"
@@ -202,7 +204,7 @@
 
 local function format(v)
 	v = v .. ''
-	return v.gsub([[(\d+\.\d{1})\d+]],'$1')
+	return gsub( v, [[(\d+\.\d{1})\d+]], '$1' )
 end
 
 function M.html(test_names,tests,results) %>
diff -r 919b9410008e -r 473e456444ff lucene/src/luan/modules/lucene/Web_search.luan
--- a/lucene/src/luan/modules/lucene/Web_search.luan	Mon Jun 01 00:51:44 2015 -0600
+++ b/lucene/src/luan/modules/lucene/Web_search.luan	Mon Jun 01 17:53:55 2015 -0600
@@ -10,6 +10,7 @@
 local Io = require "luan:Io"
 local Http = require "luan:http/Http"
 local String = require "luan:String"
+local string_to_number = String.string_to_number
 local Html = require "luan:Html"
 
 local M = {}
@@ -140,7 +141,7 @@
 			assert_double=assert_double;
 		}
 		local query = load(query_string,"<query>",query_env,true)()
-		local rows = Http.request.parameter.rows.to_number()
+		local rows = string_to_number(Http.request.parameter.rows)
 		local sort = load(Http.request.parameter.sort,"<sort>",{sort=index.query.sort},true)()
 		index.Searcher( function(searcher)
 			local results, length, total_hits = searcher.search(query,rows,sort)
diff -r 919b9410008e -r 473e456444ff scripts/test.luan
--- a/scripts/test.luan	Mon Jun 01 00:51:44 2015 -0600
+++ b/scripts/test.luan	Mon Jun 01 17:53:55 2015 -0600
@@ -19,6 +19,8 @@
 
 local error = Luan.error
 local range = Luan.range
+local trim = String.trim
+local find = String.find
 local init = Http_test.init
 local get_page = Http_test.get_page
 local Ab_testing = require "luan:lucene/Ab_testing"
@@ -37,12 +39,12 @@
 init()
 Http.request.parameter.code = "require('luan:Io').print 'hi'"
 page = get_page "/run"
-page.trim() == "hi" or error "failed"
+trim(page) == "hi" or error "failed"
 
 init()
 Http.request.parameter.cmd = "'ab'..'cd'"
 page = get_page "/shell"
-page.find "abcd" or error "failed"
+find(page,"abcd") or error "failed"
 
 init()
 page = get_page "/dump"
@@ -107,12 +109,12 @@
 init()
 Http.request.parameter.name = "bob"
 page = get_page "/examples/hi2"
-page.find "bob" or error "failed"
+find(page,"bob") or error "failed"
 
 init()
 Http.request.parameter.name = "bob"
 page = get_page "/examples/hi2_simply_html"
-page.find "bob" or error "failed"
+find(page,"bob") or error "failed"
 
 
 print "done"