changeset 147:f3686f60985d

Sort of working port of framework. Transaction bug seems to be getting in the way. Going to work around, but want the old version in the repo so I can test later.
author Mike Pavone <pavone@retrodev.com>
date Mon, 22 Nov 2010 01:15:02 -0500
parents 1f39e69446f9
children f582fd6c75ee
files basicweb.rhope cbackend_c.rhope dict.rhope framework.rhope kernel.rhope net.rhope said.rhope webserver.rhope
diffstat 8 files changed, 163 insertions(+), 68 deletions(-) [+]
line wrap: on
line diff
--- a/basicweb.rhope	Sun Nov 21 22:47:14 2010 -0500
+++ b/basicweb.rhope	Mon Nov 22 01:15:02 2010 -0500
@@ -9,7 +9,7 @@
 {
 	[args]Index[1]
 	{ port <- Int32[~] }
-	{ port <- 80 }
+	{ port <- Val[80] }
 	Print["Starting webserver"]
 	//Since we're just serving static files we don't need to setup any handlers
 	handlers <- Dictionary[]
--- a/cbackend_c.rhope	Sun Nov 21 22:47:14 2010 -0500
+++ b/cbackend_c.rhope	Mon Nov 22 01:15:02 2010 -0500
@@ -1422,7 +1422,7 @@
 	}
 }
 
-_Set List Els[text,el,index,type reg:out]
+_Set List Els C[text,el,index,type reg:out]
 {
 	out <- [[text]Append[
 		[["\tinout[1] = "
@@ -1441,7 +1441,7 @@
 	{
 		If[[~]>[3]]
 		{
-			out <- [Fold[_Set List Els[?, ?, ?, type reg], [text]Append["\trhope(FUNC_List, inout, 0, 1);\n"], value]
+			out <- [Fold[_Set List Els C[?, ?, ?, type reg], [text]Append["\trhope(FUNC_List, inout, 0, 1);\n"], value]
 				]Append[[["\t_const_"]Append[Escape Rhope Name[name,[type reg]Escape Pattern >>]]]Append[" = inout[0];\n"]]
 		}{
 			Const Construct C[value,type reg]
--- a/dict.rhope	Sun Nov 21 22:47:14 2010 -0500
+++ b/dict.rhope	Mon Nov 22 01:15:02 2010 -0500
@@ -282,6 +282,16 @@
 	out <- Print["Dictionary\n\t{Empty}"]
 }
 
+New Like@Dictionary[in:out]
+{
+	out <- Dictionary[]
+}
+
+New Like@Empty Dictionary[in:out]
+{
+	out <- in
+}
+
 Length@Empty Dictionary[dict:out]
 {
 	out <- 0
--- a/framework.rhope	Sun Nov 21 22:47:14 2010 -0500
+++ b/framework.rhope	Mon Nov 22 01:15:02 2010 -0500
@@ -1,38 +1,65 @@
 Import webserver.rhope
 
+_Key Value Map[list,index,newlist,worker:out]
+{
+	newval,newkey <- [worker]Call[[list]Index[index], index]
+	
+	next <- [newlist]Set[newkey, newval]
+	
+	[list]Next[index]
+	{
+		out <- _Key Value Map[list, ~, next, worker]
+	}{
+		out <- Val[next]
+	}
+}
+
+Key Value Map[list,worker:out]
+{
+	[list]First
+	{
+		out <- _Key Value Map[list, ~, New Like[list], worker]
+	}{
+		out <- New Like[list]
+	}
+}
+
 Framework Handler[con,path,request type,queryvars,headers,handler,title,use session]
 {
 	page <- Page[title, path, use session, queryvars, headers]
-	out list <- [handler]Do[ [[List[]]Append[page]]Append[path] ]
-	handler page <- [out list]Index[0]
+	handler page <- [handler]Call[page,path]
 	If[[request type] = ["POST"]]
 	{
-		final page <- Process POST[handler page, con, headers]
+		final page,ncon <- Process POST[handler page, con, headers]
 	}{
 		final page <- Val[handler page]
+		ncon <- Val[con]
 	}
 	string,out headers <- [final page]Render
 	
-	[HTTP OK[con, Get Content Type[".html"], [string]Length, out headers]
-	]Put String[string]
+	after headers <- HTTP OK[ncon, Get Content Type[".html"], [string]Byte Length, out headers]
+	{ Print["Sent status"]
+	{ [[string]Write to File[after headers]]Close
+	{ Print["Wrote data"] }}}
 }
 
 Handler Fixer[handler:out]
 {
-	If[[Type Of[handler]] = ["List"]]
+	[(List(),List Leaf())]Find[=[?,Blueprint Of[handler]]]
 	{
-		out <- [[["Framework Handler"]Set Input[5, [handler]Index[0]]]Set Input[6, [handler]Index[1]]]Set Input[7, [handler]Index[2]]
+		out <- Val[Framework Handler[?, ?, ?, ?, ?, [handler]Index[0], [handler]Index[1], [handler]Index[2]]]
 	}{
 		out <- handler
 	}
 }
 
-Start Web[handlers]
+Start Web[handlers,port]
 {
 	Print["Starting Rhope Web Server"]
-	Init Sessions[]
-	{ Listen on Port[80,["Connection Start"]Set Input[1, Map[handlers,"Handler Fixer"]]] }
-	Wait Forever[]
+	workaround <- Init Sessions[]
+	Val[workaround]
+	{ Listen on Port[port,Connection Start[?, ?, Map[handlers,Handler Fixer[?]]]]
+	{ Wait Forever[] }}
 }
 
 Get Class[container:class]
@@ -54,7 +81,7 @@
 
 Web Event[name,origin,data:out]
 {
-	out <- [[[Build["Web Event"]]Event Name <<[name]]Origin <<[origin]]Data <<[data]	
+	out <- [[[Build[Web Event()]]Event Name <<[name]]Origin <<[origin]]Data <<[data]
 }
 
 Blueprint Web Container
@@ -72,7 +99,7 @@
 
 Web Container[class:out]
 {
-	out <- [[[[[[[[Build["Web Container"]
+	out <- [[[[[[[[Build[Web Container()]
 	]Tag Name <<["div"]
 	]Class <<[class]
 	]Propagate Events <<[No]
@@ -95,11 +122,11 @@
 
 _Preformatted[child,val:out]
 {
-	If[[Type Of[child]] = ["Web Text"]]
+	If[[Blueprint Of[child]] = [Web Text()]]
 	{
 		out <- [child]Preformatted <<[val]
 	}{
-		If[[Type Of[child]] = ["Web Container"]]
+		If[[Blueprint Of[child]] = [Web Container()]]
 		{
 			out <- [child]Preformatted[val]
 		}{
@@ -110,7 +137,7 @@
 
 Preformatted@Web Container[cont,preformatted?:out]
 {
-	out <- [[cont]Children <<[ Map[[cont]Children >>, ["_Preformatted"]Set Input[1, preformatted?]] ]
+	out <- [[cont]Children <<[ Map[[cont]Children >>, _Preformatted[?, preformatted?]] ]
 	]Preformatted <<[preformatted?]
 }
 
@@ -120,7 +147,7 @@
 				[
 					[container]Use Session <<[Yes]
 				]Session <<[session]
-			]Children <<[ Map[ [container]Children >>, ["Set Session"]Set Input[1, session] ] ]
+			]Children <<[ Map[ [container]Children >>, Set Session[?, session] ] ]
 }
 
 Set Handler@Web Container[container,event name,handler:out]
@@ -141,7 +168,7 @@
 	out <- [[[[[[["<"]Append[ [container]Tag Name >> ]
 		]Append[Get Class[container]]
 		]Append[[[">"]Append[newline]]Append[tab]]
-		]Append[Fold[["Render Child"]<String@Worker, "", [container]Children >>]]
+		]Append[Fold[Render Child[?], "", [container]Children >>]]
 		]Append[[newline]Append["</"]]
 		]Append[ [container]Tag Name >> ]
 		]Append[[">"]Append[newline]]
@@ -152,18 +179,19 @@
 	event <- [events]Index[index]
 	[[container]Handlers >>]Index[ [event]Event Name >>]
 	{
-		result list <- [~]Do[
-				[[List[]]Append[container]]Append[event]
-		]
-		new container <- [result list]Index[0]
-		[result list]Index[1]
+		//The original version, you had to populate the container output
+		//and optionally populate the new event output, but that won't work
+		//now. None of my existing code really needs to populate both so I've
+		//made them mutually exclusive.
+		new container <- [~]Call[container,event]
 		{
-			out events <- [result events]Append[~]
+			out events <- Val[result events]
 		}{
-			out events <- Val[result events]
+			out events <- [result events]Append[~]
+			new container <- Val[container]
 		}
 	}{
-		new container <- container
+		new container <- Val[container]
 		out events <- Val[result events]
 	}
 	
@@ -261,7 +289,7 @@
 	
 Page[title,url,use session,queryvars,headers:out]
 {
-	page <- [[[[[[[Build["Page"]
+	page <- [[[[[[[Build[Page()]
 	]Title <<[title]
 	]URL <<[url]
 	]CSS <<[[List[]]Append["/default.css"]]
@@ -271,7 +299,7 @@
 	]Use Session <<[use session]
 	If[use session]
 	{
-		Load@Session[queryvars, headers]
+		Load Session[queryvars, headers]
 		{
 			out <- [[page]Session <<[~]]Session ID <<[ [~]Session ID>>]
 		}
@@ -302,7 +330,7 @@
 		]Append[[[page]CSS >>]Join["\">\n\t\t<link rel=\"stylesheet\" href=\""]]
 		]Append["\">\n\t</head>\n\t<body>\n\t<form method=\"POST\" action=\""]
 		]Append[[[page]Get Action]Append["\">\n"]]
-		]Append[Fold[["Render Child"]<String@Worker, "", [page]Children >>]]
+		]Append[Fold[Render Child[?], "", [page]Children >>]]
 		]Append["\t</form>\n\t</body>\n</html>"]
 	If[[page]Use Session>>]
 	{
@@ -350,7 +378,8 @@
 Decode Helper Decode[list,destlist,index:out]
 {
 	code,rest <- [[list]Index[index]]Slice[2]
-	newlist <- [destlist]Set[index, [[""]Put Byte[From Hex@Whole Number[code]]]Append[rest]]
+	decoded <- String[[Array[]]Append[Trunc UInt8[Abs UInt[Hex Int32[code]]]]]
+	newlist <- [destlist]Set[index, [decoded]Append[rest]]
 	[list]Next[index]
 	{
 		out <- Decode Helper Decode[list, newlist, ~]
@@ -392,13 +421,13 @@
 	okey <- URL Decode[key]
 }
 
-Process POST[page,con,headers:out]
+Process POST[page,con,headers:out,ncon]
 {
-	[con]Get FString[[headers]Index["Content-Length"]] {}
+	,ncon <- [con]Read[[headers]Index["Content-Length"]]
 	{
-		post string <- [~]Replace["+"," "]
+		post string <- [String[~]]Replace["+"," "]
 	}
-	post data <- Key Value Map[Dict Split[post string, "=", "&"], ["Decode Pair"]<String@Worker]
+	post data <- Key Value Map[Dict Split[post string, "=", "&"], Decode Pair[?]]
 	out <- [page]Postback[post data]
 }
 
@@ -422,7 +451,7 @@
 	
 Web Text[text,tag:out]
 {
-	out <- [[[Build["Web Text"]]Text <<[text]]Enclosing Tag <<[tag]]Preformatted <<[No]
+	out <- [[[Build[Web Text()]]Text <<[text]]Enclosing Tag <<[tag]]Preformatted <<[No]
 }
 
 Name@Web Text[text:out,none]
@@ -499,7 +528,7 @@
 
 Web Field[name,value,type:out]
 {
-	out <- [[[[Build["Web Field"]]Name <<[name]]Value <<[value]]Type <<[type]]Class <<[""]
+	out <- [[[[Build[Web Field()]]Name <<[name]]Value <<[value]]Type <<[type]]Class <<[""]
 }
 
 Set Session@Web Field[in,session:out]
@@ -545,7 +574,7 @@
 
 Web Button[name,label:out]
 {
-	out <- [[[Build["Web Button"]]Name <<[name]]Label <<[label]]Class <<[""]	
+	out <- [[[Build[Web Button()]]Name <<[name]]Label <<[label]]Class <<[""]	
 }
 
 Name@Web Button[button:name,none]
@@ -585,23 +614,25 @@
 
 Get Unique ID[:out] uses Session
 {
-	out <- [[[::ID]<Whole Number@String]Append["_"]]Append[Random[]]
-	::ID <- [::ID]+[1]
+	Print["Get Unique ID"]
+	out <- [[String[Session::ID]]Append["_"]]Append[String[Random[]]]												
+	{ Session::ID <- [Session::ID]+[1] }
 }
 
 Session[:out]
 {
-	out <- [[[[Build["Session"]]Session ID <<[Get Unique ID[]]]Use Cookies <<[No]]Data <<[Dictionary[]]]Dirty <<[No]
+	out <- [[[[Build[Session()]]Session ID <<[Get Unique ID[]]]Use Cookies <<[No]]Data <<[Dictionary[]]]Dirty <<[No]
+	{ Print["Built session"] }
 }
 
-Load@Session[queryvars,headers:out] uses Session
+Load Session[queryvars,headers:out] uses Session
 {
 	,checkquery <- [headers]Index["Cookie"]
 	{
 		parts <- Dict Split[~, "=", "; "]
 		,checkquery <- [parts]Index["session_id"]
 		{
-			,checkquery <- [::Sessions]Index[~]
+			,checkquery <- [Session::Sessions]Index[~]
 			{
 				out <- [~]Use Cookies <<[Yes]
 			}
@@ -613,7 +644,7 @@
 	{
 		,makenew <- [queryvars]Index["session_id"]
 		{
-			out, makenew <- [::Sessions]Index[~]
+			out, makenew <- [Session::Sessions]Index[~]
 		}
 	}
 	
@@ -655,14 +686,20 @@
 
 Init Sessions[:out] uses Session
 {
-	::ID <- 1
-	::Sessions <- Dictionary[]
+	Session::Sessions <- Dictionary[]
+	out <- Yes
+}
+
+Globals Session
+{
+	ID <- 1
+	Sessions <- No
 	out <- 0
 }
 
 Save@Session[session:out] uses Session
 {
-	::Sessions <- [::Sessions]Set[[session]Session ID >>, session]
+	Session::Sessions <- [Session::Sessions]Set[[session]Session ID >>, session]
 }
 
 Finalize@Session[session,headers:out headers]
@@ -684,7 +721,7 @@
 
 Web Link[text,target:out]
 {
-	out <- [[[[Build["Web Link"]]Text <<[text]]Target <<[target]]Class <<[""]]Query Params <<[Dictionary[]]	
+	out <- [[[[Build[Web Link()]]Text <<[text]]Target <<[target]]Class <<[""]]Query Params <<[Dictionary[]]	
 }
 	
 
@@ -742,7 +779,7 @@
 
 Web Table[headers,data:out]
 {
-	out <- [[Build["Web Table"]]Headers <<[headers]]Data <<[data]
+	out <- [[Build[Web Table()]]Headers <<[headers]]Data <<[data]
 }
 
 Name@Web Table[link:name,none]
@@ -770,7 +807,7 @@
 {
 	If[[[[table]Headers >>]Length] > [0]]
 	{
-		out <- [Fold[["Make Header Row"]<String@Worker, "\t\t<tr>\n", [table]Headers >>]]Append["\t\t</tr>\n"]
+		out <- [Fold[Make Header Row[?], "\t\t<tr>\n", [table]Headers >>]]Append["\t\t</tr>\n"]
 	}{
 		out <- ""
 	}
@@ -783,7 +820,7 @@
 
 Make Table Row[string,row:out]
 {
-	out <- [Fold[["Make Table Cell"]<String@Worker, [string]Append["\t\t<tr>\n"], row]]Append["\t\t</tr>"]
+	out <- [Fold[Make Table Cell[?], [string]Append["\t\t<tr>\n"], row]]Append["\t\t</tr>"]
 }
 
 Render@Web Table[table:out,headers]
@@ -791,7 +828,7 @@
 	out <- [
 				[
 					["\t<table>\n"]Append[[table]Get Header Row]
-				]Append[ Fold[["Make Table Row"]<String@Worker, "", [table]Data >>] ]
+				]Append[ Fold[Make Table Row[?], "", [table]Data >>] ]
 			]Append["\t</table>\n"]
 }
 
@@ -801,9 +838,9 @@
 	Alt
 }
 
-New@Web Image[source,alt:out]
+Web Image[source,alt:out]
 {
-	out <- [[Build["Web Image"]]Source <<[source]]Alt <<[alt]
+	out <- [[Build[Web Image()]]Source <<[source]]Alt <<[alt]
 }
 
 Name@Web Image[image:name,none]
--- a/kernel.rhope	Sun Nov 21 22:47:14 2010 -0500
+++ b/kernel.rhope	Mon Nov 22 01:15:02 2010 -0500
@@ -302,3 +302,46 @@
 	out <- Pause[Resume[?,Yes]]
 }
 
+Foreign C:libc
+{
+	rand[:out(Int32,Naked)]
+	srand[seed(Int32,Naked)]
+	time[storage(Int64,Raw Pointer,Mutable):storage]
+}
+
+Globals _rand
+{
+	seeded <- No
+}
+
+_Seeded?[:yep,nope] uses _rand
+{
+	Print["Seeded"]
+	yep,nope <- If[_rand::seeded] {}
+	{
+		_rand::seeded <- Yes
+	}
+}
+
+_srand[val:out]
+{
+	srand[]
+	out <- Yes
+}
+
+Random[:out]
+{
+	Print["Random"]
+	yep,nope <- _Seeded?[]
+	Val[yep]
+	{
+		_srand[Trunc Int32[time[0i64]]]
+		{ out <- rand[] }
+	}
+	Val[nope]
+	{
+		out <- rand[]
+	}
+}
+
+
--- a/net.rhope	Sun Nov 21 22:47:14 2010 -0500
+++ b/net.rhope	Mon Nov 22 01:15:02 2010 -0500
@@ -433,7 +433,7 @@
 		{
 			data,out con,err <- [con]_Read Delim[delim,poffset,buflist]
 		}{
-			err <- [Fold[_Add Len[?], 0, buflist]]+[Length[[con]Buffer >>]]
+			err <- [Fold[_Add Len[?], 0, buflist]]+[Length[[con]Read Buffer >>]]
 		}
 	}{
 		,checknew <- If[[poffset]>=[0]]
@@ -441,18 +441,18 @@
 			If[[[delim]Length]>[[outbuf]Length]]
 			{
 				//Avoid possibility of having to check across more than 2 buffers
-				data,out con,err <-[[con]Buffer <<[
-						_Merge Buffers[[[buflist]Append[[con]Buffer >>]]Append[outbuf]]
+				data,out con,err <-[[con]Read Buffer <<[
+						_Merge Buffers[[[buflist]Append[[con]Read Buffer >>]]Append[outbuf]]
 					]
 				]Read Delim[delim]
 			}{
-				,checknew <- _Check Split[[con]Buffer >>, outbuf, delim ,poffset]
+				,checknew <- _Check Split[[con]Read Buffer >>, outbuf, delim ,poffset]
 				{
 					before <- [_internal_array_copychunk[[con]Read Buffer >>, 0, _internal_array_allocnaked[~, UInt8()], 0, ~]
 					]Length <<[~]
 					
 					data <- _Merge Buffers[[buflist]Append[before]]
-					after off <- [[delim]Length]- [[[[con]Buffer >>]Length]-[~]]
+					after off <- [[delim]Length]- [[[[con]Read Buffer >>]Length]-[~]]
 					rbufferlen <- [[outbuf]Length]-[after off]
 					out con <- [con]Read Buffer <<[
 						[_internal_array_copychunk[outbuf, after off, 
--- a/said.rhope	Sun Nov 21 22:47:14 2010 -0500
+++ b/said.rhope	Mon Nov 22 01:15:02 2010 -0500
@@ -17,7 +17,7 @@
 	//Then we empty the page object
 	]Clear Children
 	//And we add a hyperlink
-	]Add Child[New@Web Link["Click Here","/said"]]
+	]Add Child[Web Link["Click Here","/said"]]
 }
 
 //This worker defines our page
@@ -30,17 +30,20 @@
 		out <- [page]Add Child[ ["You said: "]Append[~] ]
 	}{
 		//Otherwise we provide them with a form to enter a word
-		out <- [[[page]Add Child[New@Web Field["foo","","text"]]
+		out <- [[[page]Add Child[Web Field["foo","","text"]]
 			]Add Child[
-				New@Web Button["blah","Click!"]
+				Web Button["blah","Click!"]
 			//The Set Handler call here attaches the worker Save Text to the click event for the page
 			//Events propagate can propagate themselves up the page hierarchy and handled wherever appropriate
 			]]Set Handler["click","Save Text"]
 	}
 }
 
-Main[]
+Main[args]
 {
+	[args]Index[1]
+	{ port <- Int32[~] }
+	{ port <- Val[80] }
 	//Start Web starts the webserver and initializes the web framework
 	Start Web[
 		//It takes a dictionary mapping paths to Workers that define dynamic pages
@@ -48,6 +51,6 @@
 		//that allows the framework to take care of some of the drudgework for us
 		//The first element in the list is the worker name, the second the page title 
 		//and the third element indicates whether this page will be using session data or not
-		[New@Dictionary[]]Set["/said",("Said","That's what she said",Yes)]
+		[Dictionary[]]Set["/said",[(0,"That's what she said",Yes)]Set[0,Said[?]]], port
 	]
 }
--- a/webserver.rhope	Sun Nov 21 22:47:14 2010 -0500
+++ b/webserver.rhope	Mon Nov 22 01:15:02 2010 -0500
@@ -95,7 +95,7 @@
 	string <- "<html><head><title>Document Not Found</title></head><body>The document you requested is not available on this server.</body></html>"
 	HTTP Response[client, Get Content Type[".html"], [string]Length, Dictionary[], "404 Not Found"]
 	{
-		[string]Write to File[~]
+		[[string]Write to File[~]]Close
 	}
 }
 
@@ -134,8 +134,9 @@
 				If[[content length] > [0]]
 				{
 					data <- [file]Read[content length]
-					[HTTP OK[client, Get Content Type[path], content length, Dictionary[]]
+					[[HTTP OK[client, Get Content Type[path], content length, Dictionary[]]
 					]Write[data]
+					]Close
 					{ Close[file] }
 				}{
 					HTTP Not Found[client]
@@ -161,6 +162,7 @@
 
 Connection Start[con,address,handlers]
 {
+	Print["New connection!"]
 	,client <- [con]Read Delim[["\r\n"]Buffer >>]
 	{ request <- String[~] }
 	parts <- [request]Split[" "]