summaryrefslogtreecommitdiff
path: root/Lib/ocaml/ocaml.swg
blob: 396539a99ef9422a42cb9c5ab4a23aa683740f9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/* -*-c-*- */

/* SWIG pointer structure */

#ifdef __cplusplus
extern "C" {
#endif

#define C_bool 0
#define C_char 1
#define C_uchar 2
#define C_short 3
#define C_ushort 4
#define C_int 5
#define C_uint 6
#define C_int32 7
#define C_int64 8
#define C_float 9
#define C_double 10
#define C_ptr 11
#define C_array 12
#define C_list 13
#define C_obj 14
#define C_string 15
#define C_enum 16

/* Cast a pointer if possible; returns 1 if successful */
    
    SWIGSTATIC int
    SWIG_Cast (void *source, swig_type_info *source_type,
	       void **ptr, swig_type_info *dest_type)
    {
	if (dest_type != source_type) {
	    /* We have a type mismatch.  Will have to look through our type
	       mapping table to figure out whether or not we can accept this
	       datatype.  */
	    if( !dest_type || !source_type ) {
		*ptr = source;
		return 0;
	    } else {
		swig_type_info *tc = 
		    SWIG_TypeCheck( (char *)source_type->name, dest_type );
		if( tc ) {
		    *ptr = SWIG_TypeCast( tc, source );
		    return 0;
		} else
		    return -1;
	    }
	} else {
	    *ptr = source;
	    return 0;
	}
    }

/* Return 0 if successful. */
    SWIGSTATIC int
    SWIG_GetPtr(void *inptr, void **outptr, 
		swig_type_info *intype, swig_type_info *outtype) {
	CAMLparam0();
	if (intype) {
	    return !SWIG_Cast(inptr, intype,
			      outptr, outtype);
	} else {
	    *outptr = inptr;
	    return 0;
	}
    }

    SWIGSTATIC void caml_print_list( value v );

    SWIGSTATIC void caml_print_val( value v ) {
	switch( Tag_val(v) ) {
	case C_bool:
	    if( Bool_val(Field(v,0)) ) fprintf( stderr, "true " );
	    else fprintf( stderr, "false " );
	    break;
	case C_char:
	case C_uchar:
	    fprintf( stderr, "'%c' (\\%03d) ", 
		     (Int_val(Field(v,0)) >= ' ' &&
		      Int_val(Field(v,0)) < 127) ? Int_val(Field(v,0)) : '.',
		     Int_val(Field(v,0)) );
	    break;
	case C_short:
	case C_ushort:
	case C_int:
	    fprintf( stderr, "%d ", (int)caml_long_val(v) );
	    break;

	case C_uint:
	case C_int32:
	    fprintf( stderr, "%ud ", (unsigned int)caml_long_val(v) );
	    break;
	case C_int64:
	    fprintf( stderr, "%ld ", caml_long_val(v) );
	    break;
	case C_float:
	case C_double:
	    fprintf( stderr, "%f ", caml_double_val(v) );
	    break;

	case C_ptr:
	    fprintf( stderr, "PTR(%p) ", caml_ptr_val(v,0) );
	    break;
	case C_array:
	{
	    unsigned int i;
	    for( i = 0; i < Wosize_val( Field(v,0) ); i++ ) 
		caml_print_val( Field(Field(v,0),i) );
	}
	break;
	case C_list:
	    caml_print_list( Field(v,0) );
	    break;
	case C_obj:
	    fprintf( stderr, "OBJ(%p) ", (void *)Field(v,0) );
	    break;
	case C_string:
	    fprintf( stderr, "'%s' ", (char *)caml_ptr_val(v,0) );
	    break;
	}
    }

    SWIGSTATIC void caml_print_list( value v ) {
	CAMLparam1(v);
	while( v && Is_block(v) ) {
	    fprintf( stderr, "[ " );
	    caml_print_val( Field(v,0) );
	    fprintf( stderr, "]\n" );
	    v = Field(v,1);
	}
    }

    SWIGSTATIC value caml_list_nth( value lst, int n ) {
	CAMLparam1(lst);
	int i = 0;
	while( i < n && lst && Is_block(lst) ) {
	    i++; lst = Field(lst,1);
	}
	if( lst == Val_unit ) CAMLreturn(Val_unit);
	else CAMLreturn(Field(lst,0));
    }
    
    SWIGSTATIC value caml_list_append( value lst, value elt ) {
	CAMLparam2(lst,elt);
	CAMLlocal3(v,vt,lh);
	lh = Val_unit;
	v = Val_unit;

	/* Appending C_void should have no effect */
	if( !Is_block(elt) ) return lst;

	while( lst && Is_block(lst) ) {
	    if( v && v != Val_unit ) {
		vt = alloc_tuple(2);
		Store_field(v,1,vt);
		v = vt;
	    } else {
		v = lh = alloc_tuple(2);
	    }
	    Store_field(v,0,Field(lst,0));
	    lst = Field(lst,1);
	}

	if( v && Is_block(v) ) {
	    vt = alloc_tuple(2);
	    Store_field(v,1,vt);
	    v = vt;
	} else {
	    v = lh = alloc_tuple(2);
	}
	Store_field(v,0,elt);
	Store_field(v,1,Val_unit);

	CAMLreturn(lh);
    }

    SWIGSTATIC int caml_list_length( value lst ) {
	CAMLparam1(lst);
	int i = 0;
	while( lst && Is_block(lst) ) { i++; lst = Field(lst,1); }
	CAMLreturn(i);
    }

#ifdef __cplusplus
    namespace caml {
	extern "C"
#endif 
	    value alloc(int,int);
#ifdef __cplusplus
    };
#endif

    SWIGSTATIC value caml_swig_alloc(int x,int y) {
#ifdef __cplusplus
	using namespace caml;
#endif
	return alloc(x,y);
    }
    
    SWIGSTATIC value caml_val_bool( int b ) {
	CAMLparam0();
	CAMLlocal1(bv);
	bv = caml_swig_alloc(1,C_bool);
	Store_field(bv,0,Val_bool(b));
	CAMLreturn(bv);
    }

    SWIGSTATIC value caml_val_char( char c ) {
	CAMLparam0();
	CAMLlocal1(cv);
	cv = caml_swig_alloc(1,C_char);
	Store_field(cv,0,Val_int(c));
	CAMLreturn(cv);
    }

    SWIGSTATIC value caml_val_uchar( unsigned char uc ) {
	CAMLparam0();
	CAMLlocal1(ucv);
	ucv = caml_swig_alloc(1,C_uchar);
	Store_field(ucv,0,Val_int(uc));
	CAMLreturn(ucv);
    }

    SWIGSTATIC value caml_val_short( short s ) {
	CAMLparam0();
	CAMLlocal1(sv);
	sv = caml_swig_alloc(1,C_short);
	Store_field(sv,0,Val_int(s));
	CAMLreturn(sv);
    }

    SWIGSTATIC value caml_val_ushort( unsigned short us ) {
	CAMLparam0();
	CAMLlocal1(usv);
	usv = caml_swig_alloc(1,C_ushort);
	Store_field(usv,0,Val_int(us));
	CAMLreturn(usv);
    }

    SWIGSTATIC value caml_val_int( int i ) {
	CAMLparam0();
	CAMLlocal1(iv);
	iv = caml_swig_alloc(1,C_int);
	Store_field(iv,0,Val_int(i));
	CAMLreturn(iv);
    }

    SWIGSTATIC value caml_val_uint( unsigned int ui ) {
	CAMLparam0();
	CAMLlocal1(uiv);
	uiv = caml_swig_alloc(1,C_int);
	Store_field(uiv,0,Val_int(ui));
	CAMLreturn(uiv);
    }

    SWIGSTATIC value caml_val_long( long l ) {
	CAMLparam0();
	CAMLlocal1(lv);
	lv = caml_swig_alloc(1,C_int64);
	Store_field(lv,0,copy_int64(l));
	CAMLreturn(lv);
    }

    SWIGSTATIC value caml_val_ulong( unsigned long ul ) {
	CAMLparam0();
	CAMLlocal1(ulv);
	ulv = caml_swig_alloc(1,C_int64);
	Store_field(ulv,0,copy_int64(ul));
	CAMLreturn(ulv);
    }

    SWIGSTATIC value caml_val_float( float f ) {
	CAMLparam0();
	CAMLlocal1(fv);
	fv = caml_swig_alloc(1,C_float);
	Store_field(fv,0,copy_double(f));
	CAMLreturn(fv);
    }

    SWIGSTATIC value caml_val_double( double d ) {
	CAMLparam0();
	CAMLlocal1(fv);
	fv = caml_swig_alloc(1,C_double);
	Store_field(fv,0,copy_double(d));
	CAMLreturn(fv);
    }

    SWIGSTATIC value caml_val_ptr( void *p, swig_type_info *info ) {
	CAMLparam0();
	CAMLlocal1(vv);
	vv = caml_swig_alloc(2,C_ptr);
	Store_field(vv,0,copy_int64((long)p));
	Store_field(vv,1,copy_int64((long)info));
	CAMLreturn(vv);
    }

    SWIGSTATIC value caml_val_string( char *p ) {
	CAMLparam0();
	CAMLlocal1(vv);
	if( !p ) CAMLreturn(caml_val_ptr( (void *)p, 0 ));
	vv = caml_swig_alloc(1,C_string);
	Store_field(vv,0,copy_string(p));
	CAMLreturn(vv);
    }

    SWIGSTATIC value caml_val_string_len( char *p, int len ) {
	CAMLparam0();
	CAMLlocal1(vv);
	if( !p || len < 0 ) CAMLreturn(caml_val_ptr( (void *)p, 0 ));
	vv = caml_swig_alloc(1,C_string);
	Store_field(vv,0,alloc_string(len));
	memcpy(String_val(Field(vv,0)),p,len);
	CAMLreturn(vv);
    }

    SWIGSTATIC value caml_val_obj( void *v, char *object_type ) {
	CAMLparam0();
	CAMLreturn(callback2(*caml_named_value("caml_create_object_fn"),
			     caml_val_ptr(v,SWIG_TypeQuery(object_type)),
			     copy_string(object_type)));
    }

    SWIGSTATIC long caml_long_val_full( value v, char *name ) {
	CAMLparam1(v);
	if( !Is_block(v) ) return 0;

	switch( Tag_val(v) ) {
	case C_bool:
	case C_char:
	case C_uchar:
	case C_short:
	case C_ushort:
	case C_int:
	    CAMLreturn(Int_val(Field(v,0)));
	case C_uint:
	case C_int32:
	    CAMLreturn(Int32_val(Field(v,0)));
	case C_int64:
	    CAMLreturn((long)Int64_val(Field(v,0)));
	case C_float:
	case C_double:
	    CAMLreturn((long)Double_val(Field(v,0)));
	case C_string:
	    CAMLreturn((long)String_val(Field(v,0)));
	case C_ptr:
	    CAMLreturn((long)Int64_val(Field(Field(v,0),0)));
	case C_enum: {
	    CAMLlocal1(ret);
	    value *enum_to_int = caml_named_value(SWIG_MODULE "_enum_to_int");
	    if( !name ) failwith( "Not an enum conversion" );
	    ret = callback2(*enum_to_int,*caml_named_value(name),v);
	    CAMLreturn(caml_long_val(ret));
	}
	default:
	    failwith("No conversion to int");
	}
    }

    SWIGSTATIC long caml_long_val( value v ) {
	return caml_long_val_full(v,0);
    }

    SWIGSTATIC double caml_double_val( value v ) {
	CAMLparam1(v);
	if( !Is_block(v) ) return 0.0;
	switch( Tag_val(v) ) {
	case C_bool:
	case C_char:
	case C_uchar:
	case C_short:
	case C_ushort:
	case C_int:
	    CAMLreturn(Int_val(Field(v,0)));
	case C_uint:
	case C_int32:
	    CAMLreturn(Int32_val(Field(v,0)));
	case C_int64:
	    CAMLreturn(Int64_val(Field(v,0)));
	case C_float:
	case C_double:
	    CAMLreturn(Double_val(Field(v,0)));
	default:
	    fprintf( stderr, "Unknown block tag %d\n", Tag_val(v) );
	    failwith("No conversion to double");
	}
    }

    SWIGSTATIC int caml_ptr_val_internal( value v, void **out, 
				      swig_type_info *descriptor ) {
	CAMLparam1(v);
	void *outptr = NULL;
        swig_type_info *outdescr = NULL;

	if( !Is_block(v) ) return -1;
	switch( Tag_val(v) ) {
	case C_obj:
	    return caml_ptr_val_internal
		(callback(*caml_named_value("caml_obj_ptr"),v),out,descriptor);
	case C_string:
	    outptr = (void *)String_val(Field(v,0));
	    break;
	case C_ptr:
	    outptr = (void *)(long)Int64_val(Field(v,0));
            outdescr = (swig_type_info *)(long)Int64_val(Field(v,1));
	    break;
	default:
	    *out = 0;
	    CAMLreturn(1);
	    break;
	}
	
	CAMLreturn(SWIG_GetPtr(outptr,out,descriptor,outdescr));
    }

    SWIGSTATIC void *caml_ptr_val( value v, swig_type_info *descriptor ) {
	CAMLparam0();
	void *out = NULL;
	if( !caml_ptr_val_internal( v, &out, descriptor ) )
	    CAMLreturn(out);
	else
	    failwith( "No appropriate conversion found." );
    }

    SWIGSTATIC char *caml_string_val( value v ) {
	return (char *)caml_ptr_val( v, 0 );
    }

    SWIGSTATIC int caml_bool_check( value v ) {
	CAMLparam1(v);
	
	if( !Is_block(v) ) return 0;
	
	switch( Tag_val(v) ) {
	case C_bool:
	case C_ptr:
	case C_string:
	    CAMLreturn(1);
	default:
	    CAMLreturn(0);
	}
    }

    SWIGSTATIC int caml_int_check( value v ) {
	CAMLparam1(v);
	
	if( !Is_block(v) ) return 0;
	
	switch( Tag_val(v) ) {
	case C_char:
	case C_uchar:
	case C_short:
	case C_ushort:
	case C_int:
	case C_uint:
	case C_int32:
	case C_int64:
	    CAMLreturn(1);

	default:
	    CAMLreturn(0);
	}
    }

    SWIGSTATIC int caml_float_check( value v ) {
	CAMLparam1(v);
	if( !Is_block(v) ) return 0;

	switch( Tag_val(v) ) {
	case C_float:
	case C_double:
	    CAMLreturn(1);

	default:
	    CAMLreturn(0);
	}	
    }

    SWIGSTATIC int caml_ptr_check( value v ) {
	CAMLparam1(v);
	if( !Is_block(v) ) return 0;

	switch( Tag_val(v) ) {
	case C_string:
	case C_ptr:
	case C_int64:
	    CAMLreturn(1);

	default:
	    CAMLreturn(0);
	}	
    }

#ifdef __cplusplus
}
#endif