rev 470 - in trunk: pr src

SVN User <[email protected]> Wed, 12 May 2004 16:10:17 -0400
Newsgroups gmane.comp.lang.prothon.cvs
Message-ID <[email protected]>
Author: mark
Date: 2004-05-12 16:10:14 -0400 (Wed, 12 May 2004)
New Revision: 470

Modified:
   trunk/pr/test.pr
   trunk/src/interp.c
Log:
fixed default reference to self when not in function call

Modified: trunk/pr/test.pr
===================================================================
--- trunk/pr/test.pr	2004-05-12 19:54:05 UTC (rev 469)
+++ trunk/pr/test.pr	2004-05-12 20:10:14 UTC (rev 470)
@@ -1,46 +1,31 @@
 #!/usr/bin/env prothon
 
-# dbm.pr
+# bin.pr
 
-from DBM import DB
+x = Exception
 
-db = DB("test.dbm", mode=DBM.RWTRUNCATE)
+object obj(String, Int):
+        Int.init_{obj}(99)      # inits using second proto
+        print Int.str_{obj}()   # prints 99
+        
+try:
+    with obj: Int.init_{obj}(99)   # init Int a second time
+except x,e: print e    # throws "object has binary data, expected none"
 
-if db.exists?('a'):
-	print 'err1'
-	Sys.exit(1)
-	
-db.store('a', 'abc' + 0.chr() + 'def')
-db.store('c', 'xyz')
-db.store('b', '')
+try:
+    obj.init_('abc') # init as String
+except x,e: print e     # throws "object has binary data, expected none"
 
-if not db.exists?('a'):
-	print 'err2'
-	Sys.exit(1)
-	
-if db.fetch('a') != 'abc' + 0.chr() + 'def':
-	print 'err3'
-	Sys.exit(1)
-	
-if db.fetch('b') != '':
-	print 'err4'
-	Sys.exit(1)
-	
-if db.fetch('c') != 'xyz':
-	print 'err5'
-	Sys.exit(1)
-	
-list = []
-k = db.firstKey()
-while k:
-	list.append!(k)
-	k = db.nextKey(k)
+L = Proto(List) # L is uninitialized list
 
-list.sort!()
+L.init_(1,2,3)
+print L             # prints [1, 2, 3] 
 
-if list != ['a','b','c']:
-	print 'err6'
-	Sys.exit(1)
-	
-print '\nall tests passed\n'
+L.init_(4,5,6)
+print L             # prints [4, 5, 6] 
 
+d = {1:2, 'a':'b'}
+print d             # prints {1:2, 'a':'b'}
+
+d.init_(c = 'd', f = 'g')
+print d             # prints {f:'g', c:'d'} 
\ No newline at end of file

Modified: trunk/src/interp.c
===================================================================
--- trunk/src/interp.c	2004-05-12 19:54:05 UTC (rev 469)
+++ trunk/src/interp.c	2004-05-12 20:10:14 UTC (rev 470)
@@ -798,6 +798,10 @@
 					return_value = fr_pop; 
 				break;
 			case OP_PUSH_SELF:
+				if (!frame->self) {
+					raise_exception(ist, OBJ(INTERPRETER_EXC), "self only valid in function");
+					break;
+				}
 				fr_push(frame->self);
 				break;
 			case OP_PUSH_CALLER:
@@ -808,6 +812,10 @@
 				fr_push(frame->dyn_locals);  
 				break;
 			case OP_PUSH_SELF_REF: 
+				if (!frame->self) {
+					raise_exception(ist, OBJ(INTERPRETER_EXC), "self.var only valid in function");
+					break;
+				}
 				fr_push(frame->self);  
 				fr_push(fr_data(1)); 
 				break;
@@ -1042,8 +1050,12 @@
 					if (!func_obj->data.ptr) goto no_func_ptr;
 					plist = get_func_params(ist, func_obj, param*2, fr_stack+fr_sp+2);
 					if ( !(self = fr_stack[fr_sp+1]) &&
-						 !(self = get_attr(ist, func_obj, SYM(BINDOBJ_))) )
-						self = frame->self; 
+						 !(self = get_attr(ist, func_obj, SYM(BINDOBJ_))) &&
+						 !(self = frame->self) &&
+						 !(self = ist->module) ) {
+						 raise_exception(ist, OBJ(INTERNAL_EXC), "no self for this function");
+						 break;
+					}
 					IF_EXC_BREAK;
 					pre_call_lock(ist, self, plist); IF_EXC_BREAK;
 					res = ((pr_func*)(func_obj->data.ptr))
@@ -1062,8 +1074,12 @@
 					if (!func_obj->data.ptr) goto no_func_ptr;
 					new_locals = NEW_OBJ(NULL);
 					if ( !(self = fr_stack[fr_sp+1]) &&
-						 !(self = get_attr(ist, func_obj, SYM(BINDOBJ_))) )
-						self = frame->self; 
+						 !(self = get_attr(ist, func_obj, SYM(BINDOBJ_))) &&
+						 !(self = frame->self) &&
+						 !(self = ist->module) ) {
+						 raise_exception(ist, OBJ(INTERNAL_EXC), "no self for this function");
+						 break;
+					}
 					IF_EXC_BREAK;
 					new_frame = create_frame( ist, self, NULL, frame->locals, 
 						                           new_locals, func_obj, NULL );
@@ -1198,8 +1214,13 @@
 					plist = get_func_params(ist, func_obj, param*2, fr_stack+fr_sp+3);
 					IF_EXC_BREAK;
 					if ( !(self = fr_stack[fr_sp+2]) &&
-						 !(self = get_attr(ist, func_obj, SYM(BINDOBJ_))) )
-						self = fr_stack[fr_sp]; 
+						 !(self = get_attr(ist, func_obj, SYM(BINDOBJ_))) &&
+						 !(self = fr_stack[fr_sp]) &&
+						 !(self = frame->self) &&
+						 !(self = ist->module) ) {
+						 raise_exception(ist, OBJ(INTERNAL_EXC), "no self for this function");
+						 break;
+					}
 					IF_EXC_BREAK;
 					pre_call_lock(ist, self, plist);  IF_EXC_BREAK;
 					pr_assert(sizeof(obj_p) == sizeof(clist_item_t));
@@ -1219,8 +1240,13 @@
 					if (!func_obj->data.ptr) goto no_func_ptr2;
 					new_locals = NEW_OBJ(NULL);
 					if ( !(self = fr_stack[fr_sp+2]) &&
-						 !(self = get_attr(ist, func_obj, SYM(BINDOBJ_))) )
-						self = fr_stack[fr_sp];
+						 !(self = get_attr(ist, func_obj, SYM(BINDOBJ_))) &&
+						 !(self = fr_stack[fr_sp]) &&
+						 !(self = frame->self) &&
+						 !(self = ist->module) ) {
+						 raise_exception(ist, OBJ(INTERNAL_EXC), "no self for this function");
+						 break;
+					}
 					IF_EXC_BREAK;
 					new_frame = create_frame( ist, self, NULL, frame->locals, 
 						                           new_locals, func_obj, NULL );