Index: tcc4tcl.tcl ================================================================== --- tcc4tcl.tcl +++ tcc4tcl.tcl @@ -149,21 +149,29 @@ set args [list] # If we aren't creating a new interp, it must be the first argument # If the definition of this proc already includes the interp # argument, use it -- otherwise add one - if {[lindex $adefs 0] != "Tcl_Interp*"} { - set newInterp 1 - } else { - set newInterp 0 - set interp_name [lindex $adefs 1] + set newInterp 1 + foreach {type var} $adefs { + if {$type == "Tcl_Interp*"} { + set newInterp 0 + set interp_name $var + + break + } } # Create the C-style argument definition foreach {type var} $adefs { lappend adefs_c [list $type $var] set types($var) $type + + if {$type == "Tcl_Interp*"} { + continue + } + lappend args $var } set adefs_c [join $adefs_c {, }] @@ -209,18 +217,14 @@ ## If we need to create a new interpreter, do so if {$newInterp} { set interp_name "ip" _ccode $handle " Tcl_Interp *${interp_name};" - - set args_nointerp $args - } else { - set args_nointerp [lrange $args 1 end] } # Declare Tcl_Obj variables - _ccode $handle " Tcl_Obj *_[join $args_nointerp {, *_}];" + _ccode $handle " Tcl_Obj *_[join $args {, *_}];" _ccode $handle "" if {$newInterp} { _ccode $handle " ${interp_name} = Tcl_CreateInterp();" @@ -227,11 +231,11 @@ _ccode $handle " if (!${interp_name}) $return_failure;" _ccode $handle "" } # Process all arguments - foreach arg $args_nointerp { + foreach arg $args { set type $types($arg) switch -- $type { int - long - Tcl_WideInt - float - double { switch -- $type { float { @@ -261,11 +265,11 @@ _ccode $handle " if (!Tcl_ObjSetVar2(${interp_name}, Tcl_NewStringObj(\"${arg}\", -1), NULL, _$arg, 0)) $return_failure;" } _ccode $handle "" _ccode $handle " tclrv = Tcl_Eval($interp_name, \"$cbody\");" - _ccode $handle " if (tclrv != TCL_OK) $return_failure;" + _ccode $handle " if (tclrv != TCL_OK && tclrv != TCL_RETURN) $return_failure;" _ccode $handle "" if {$rtype != "ok" && $rtype != "void"} { _ccode $handle " rv_interp = Tcl_GetObjResult(${interp_name});" } @@ -295,12 +299,12 @@ _ccode $handle " if (Tcl_GetDoubleFromObj(ip, rv_interp, &rv) != TCL_OK) $return_failure;" } char* { _ccode $handle " rv = Tcl_GetString(rv_interp);" } - char* { - _ccode $handle " rv = Tcl_GetString(rv_interp);" + Tcl_Obj* { + _ccode $handle " rv = rv_interp;" } } _ccode $handle "" if {$rtype != "void"} { Index: test.tcl ================================================================== --- test.tcl +++ test.tcl @@ -145,15 +145,16 @@ curl_fetch http://rkeene.org/ } set handle [tcc4tcl::new] -$handle proc callToTcl {int a int b} int { +$handle proc callToTcl {Tcl_Interp* ip int a int b} int { set retval [expr {$a + $b}] return $retval } -$handle cwrap callToTcl {int a int b} int +$handle cwrap callToTcl {Tcl_Interp* ip int a int b} int +puts [$handle code] $handle go if {[callToTcl 3 5] != 8} { - error "3 + 5 is 8" + error "3 + 5 is 8, not [callToTcl 3 5]" }