//----------------------------------------------------------------------
//This code is developed as part of the Java CoG Kit project
//The terms of the license can be found at http://www.cogkit.org/license
//This message may not be removed or altered.
//----------------------------------------------------------------------

/*
 * Created on Dec 26, 2006
 */
package org.globus.cog.karajan.compiled.nodes.functions;

import k.rt.ExecutionException;
import k.rt.Stack;

import org.globus.cog.karajan.analyzer.ArgRef;
import org.globus.cog.karajan.analyzer.Param;
import org.globus.cog.karajan.util.TypeUtil;

public class KException extends AbstractSingleValuedFunction {
    private ArgRef<String> message;
    private ArgRef<Throwable> exception;
   
	@Override
	protected Param[] getParams() {
		return params("message", "exception");
	}

	public Object function(Stack stack) { 
		Object message = this.message.getValue(stack);
		Throwable detail = this.exception.getValue(stack);
		if (message instanceof String) {
			return new ExecutionException(this, (String) message, detail);
		}
		else if (message instanceof ExecutionException) {
			ExecutionException exc = (ExecutionException) message;
			return new ExecutionException(this, exc.getMessage(), exc);
		}
		else {
			return new ExecutionException(this, TypeUtil.toString(message));
		}
	}
}

@@ -412,16 +412,37 @@
 tions;%0A%0A
+import k.rt.Channel;%0A
 import k
@@ -524,19 +524,23 @@
 nalyzer.
-Arg
+Channel
 Ref;%0Aimp
@@ -691,28 +691,25 @@
 ction %7B%0A
-    
+%09
 private 
 ArgRef%3CS
@@ -704,76 +704,40 @@
 ate 
-ArgRef%3CString%3E message;%0A    private ArgRef%3CThrowable%3E exception
+ChannelRef%3CObject%3E c_vargs
 ;%0A   
+ 
 %0A%09@O
@@ -798,28 +798,11 @@
 ms(%22
-message%22, %22exception
+...
 %22);%0A
@@ -847,111 +847,364 @@
 k) %7B
- 
 %0A%09%09
-Object message = this.message.getValue(stack);%0A%09%09Throwable detail = this.exception.getValue(stack);
+Channel%3CObject%3E args = c_vargs.get(stack);%0A%09%09%0A%09%09if (args.size() == 0) %7B%0A%09%09%09throw new ExecutionException(this, %22Missing argument(s)%22);%0A%09%09%7D%0A%09%09if (args.size() %3E 2) %7B%0A%09%09%09throw new ExecutionException(this, %22Too many arguments%22);%0A%09%09%7D%0A%09%09Object message = args.get(0);%0A%09%09Throwable detail = null;%0A%09%09if (args.size() == 2) %7B%0A%09%09%09detail = (Throwable) args.get(1);%0A%09%09%7D
 %0A%09%09i

allow non-keyword parameters to exception(): exception(message[, detail])