Check-in [af858c331d]
Overview
Comment:Updated to support a Tcl_Interp* argument in any position for "proc"
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: af858c331db7a2f716adbf34ddcbe3a97bc60b5f
User & Date: rkeene on 2014-07-16 04:05:11
Other Links: manifest | tags
Context
2014-07-16
04:09
Corrected floating point error value check-in: 56023a368d user: rkeene tags: trunk
04:05
Updated to support a Tcl_Interp* argument in any position for "proc" check-in: af858c331d user: rkeene tags: trunk
03:37
Added a "proc" sub-command to generate C stubs to call Tcl code check-in: e9115a162e user: rkeene tags: trunk
Changes

Modified tcc4tcl.tcl from [54f1e6802f] to [675d06bcbb].

147
148
149
150
151
152
153
154
155
156

157
158



159
160
161
162
163
164





165
166
167
168
169
170
171

		# Names of all arguments initialization
		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]



		}

		# Create the C-style argument definition
		foreach {type var} $adefs {
			lappend adefs_c [list $type $var]
			set types($var) $type





			lappend args $var
		}

		set adefs_c [join $adefs_c {, }]

		# Determine how to return in failure
		if {$rtype != "void"} {







<
|
|
>
|
|
>
>
>






>
>
>
>
>







147
148
149
150
151
152
153

154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179

		# Names of all arguments initialization
		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

		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 {, }]

		# Determine how to return in failure
		if {$rtype != "void"} {
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
			_ccode $handle "    $rtype rv;"
		}

		## 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 ""

		if {$newInterp} {
			_ccode $handle "    ${interp_name}  = Tcl_CreateInterp();"
			_ccode $handle "    if (!${interp_name}) $return_failure;"
			_ccode $handle ""
		}

		# Process all arguments
		foreach arg $args_nointerp {
			set type $types($arg)
			switch -- $type {
				int - long - Tcl_WideInt - float - double {
					switch -- $type {
						float {
							set convCmd Double
						}







<
<
<
<



|










|







215
216
217
218
219
220
221




222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
			_ccode $handle "    $rtype rv;"
		}

		## If we need to create a new interpreter, do so
		if {$newInterp} {
			set interp_name "ip"
			_ccode $handle "    Tcl_Interp *${interp_name};"




		}

		# Declare Tcl_Obj variables
		_ccode $handle "    Tcl_Obj *_[join $args {, *_}];"

		_ccode $handle ""

		if {$newInterp} {
			_ccode $handle "    ${interp_name}  = Tcl_CreateInterp();"
			_ccode $handle "    if (!${interp_name}) $return_failure;"
			_ccode $handle ""
		}

		# Process all arguments
		foreach arg $args {
			set type $types($arg)
			switch -- $type {
				int - long - Tcl_WideInt - float - double {
					switch -- $type {
						float {
							set convCmd Double
						}
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
				}
			}
			_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 {$rtype != "ok" && $rtype != "void"} {
			_ccode $handle "    rv_interp = Tcl_GetObjResult(${interp_name});"
		}

		switch -- $rtype {







|







263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
				}
			}
			_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 && tclrv != TCL_RETURN) $return_failure;"
		_ccode $handle ""

		if {$rtype != "ok" && $rtype != "void"} {
			_ccode $handle "    rv_interp = Tcl_GetObjResult(${interp_name});"
		}

		switch -- $rtype {
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
			}
			double {
				_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);"
			}
		}

		_ccode $handle ""
		if {$rtype != "void"} {
			_ccode $handle "    return(rv);"
		} else {







|
|







297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
			}
			double {
				_ccode $handle "    if (Tcl_GetDoubleFromObj(ip, rv_interp, &rv) != TCL_OK) $return_failure;"
			}
			char* {
				_ccode $handle "    rv = Tcl_GetString(rv_interp);"
			}
			Tcl_Obj* {
				_ccode $handle "    rv = rv_interp;"
			}
		}

		_ccode $handle ""
		if {$rtype != "void"} {
			_ccode $handle "    return(rv);"
		} else {

Modified test.tcl from [6f71a6b9f8] to [98300662c0].

143
144
145
146
147
148
149
150
151
152
153
154
155

156
157
158
159
	$handle add_library curl
	$handle go
    
	curl_fetch http://rkeene.org/
}

set handle [tcc4tcl::new]
$handle proc callToTcl {int a int b} int {
	set retval [expr {$a + $b}]

	return $retval
}
$handle cwrap callToTcl {int a int b} int

$handle go
if {[callToTcl 3 5] != 8} {
	error "3 + 5 is 8"
}







|




|
>


|

143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
	$handle add_library curl
	$handle go
    
	curl_fetch http://rkeene.org/
}

set handle [tcc4tcl::new]
$handle proc callToTcl {Tcl_Interp* ip int a int b} int {
	set retval [expr {$a + $b}]

	return $retval
}
$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, not [callToTcl 3 5]"
}