2 * The builtin VM object.
3 * Copyright (c) 1998-1999 New Generation Software (NGS) Oy
5 * Author: Markku Rossi <mtr@ngs.fi>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the Free
21 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
26 * $Source: /cygdrive/c/RCVS/CVS/ReactOS/reactos/lib/kjs/src/b_vm.c,v $
35 * VM.garbageCollect ()
38 * - properties: type mutable
40 * VM.dispatchMethod string
43 * VM.gcTrigger integer yes
44 * VM.heapAllocated integer
47 * VM.numConstants integer
48 * VM.numGlobals integer
49 * VM.stackSize integer
50 * VM.stacktraceOnError boolean yes
51 * VM.verbose integer yes
52 * VM.verboseStacktrace boolean yes
54 * VM.versionMajor integer
55 * VM.versionMinor integer
56 * VM.versionPatch integer
57 * VM.warnUndef boolean yes
63 * Types and definitions.
68 JSSymbol s_garbageCollect
;
69 JSSymbol s_stackTrace
;
71 JSSymbol s_dispatchMethod
;
75 JSSymbol s_heapAllocated
;
78 JSSymbol s_numConstants
;
79 JSSymbol s_numGlobals
;
81 JSSymbol s_stacktraceOnError
;
83 JSSymbol s_verboseStacktrace
;
85 JSSymbol s_versionMajor
;
86 JSSymbol s_versionMinor
;
87 JSSymbol s_versionPatch
;
91 typedef struct vm_ctx_st VMCtx
;
100 method (JSVirtualMachine
*vm
, JSBuiltinInfo
*builtin_info
,
101 void *instance_context
, JSSymbol method
, JSNode
*result_return
,
104 VMCtx
*ctx
= builtin_info
->obj_context
;
106 /* The default return value is undefined. */
107 result_return
->type
= JS_UNDEFINED
;
109 if (method
== ctx
->s_garbageCollect
)
111 if (args
->u
.vinteger
!= 0)
114 /* Ok, let's trigger a garbage collection. */
115 vm
->gc
.bytes_allocated
= vm
->gc
.trigger
+ 1;
117 /* ********************************************************************** */
118 else if (method
== ctx
->s_stackTrace
)
122 if (args
->u
.vinteger
== 0)
124 else if (args
->u
.vinteger
== 1)
126 if (args
[1].type
!= JS_INTEGER
)
127 goto argument_type_error
;
129 limit
= args
[1].u
.vinteger
;
134 js_vm_stacktrace (vm
, limit
);
136 /* ********************************************************************** */
137 else if (method
== vm
->syms
.s_toString
)
139 if (args
->u
.vinteger
!= 0)
142 js_vm_make_static_string (vm
, result_return
, "VM", 2);
144 /* ********************************************************************** */
146 return JS_PROPERTY_UNKNOWN
;
148 return JS_PROPERTY_FOUND
;
156 sprintf (vm
->error
, "VM.%s(): illegal amout of arguments",
157 js_vm_symname (vm
, method
));
161 sprintf (vm
->error
, "VM.%s(): illegal argument",
162 js_vm_symname (vm
, method
));
171 property (JSVirtualMachine
*vm
, JSBuiltinInfo
*builtin_info
,
172 void *instance_context
, JSSymbol property
, int set
, JSNode
*node
)
174 VMCtx
*ctx
= builtin_info
->obj_context
;
176 if (property
== ctx
->s_dispatchMethod
)
181 js_vm_make_static_string (vm
, node
,
182 vm
->dispatch_method_name
,
183 strlen (vm
->dispatch_method_name
));
185 /* ***************************************************************** */
186 else if (property
== ctx
->s_fdCount
)
191 node
->type
= JS_INTEGER
;
192 node
->u
.vinteger
= vm
->fd_count
;
194 /* ***************************************************************** */
195 else if (property
== ctx
->s_gcCount
)
200 node
->type
= JS_INTEGER
;
201 node
->u
.vinteger
= vm
->gc
.count
;
203 /* ***************************************************************** */
204 else if (property
== ctx
->s_gcTrigger
)
208 if (node
->type
!= JS_INTEGER
)
210 vm
->gc
.trigger
= node
->u
.vinteger
;
214 node
->type
= JS_INTEGER
;
215 node
->u
.vinteger
= vm
->gc
.trigger
;
218 /* ***************************************************************** */
219 else if (property
== ctx
->s_heapAllocated
)
224 node
->type
= JS_INTEGER
;
225 node
->u
.vinteger
= vm
->gc
.bytes_allocated
;
227 /* ***************************************************************** */
228 else if (property
== ctx
->s_heapFree
)
233 node
->type
= JS_INTEGER
;
234 node
->u
.vinteger
= vm
->gc
.bytes_free
;
236 /* ***************************************************************** */
237 else if (property
== ctx
->s_heapSize
)
242 node
->type
= JS_INTEGER
;
243 node
->u
.vinteger
= vm
->heap_size
;
245 /* ***************************************************************** */
246 else if (property
== ctx
->s_numConstants
)
251 node
->type
= JS_INTEGER
;
252 node
->u
.vinteger
= vm
->num_consts
;
254 /* ***************************************************************** */
255 else if (property
== ctx
->s_numGlobals
)
260 node
->type
= JS_INTEGER
;
261 node
->u
.vinteger
= vm
->num_globals
;
263 /* ***************************************************************** */
264 else if (property
== ctx
->s_stackSize
)
269 node
->type
= JS_INTEGER
;
270 node
->u
.vinteger
= vm
->stack_size
;
272 /* ***************************************************************** */
273 else if (property
== ctx
->s_stacktraceOnError
)
277 if (node
->type
!= JS_BOOLEAN
)
279 vm
->stacktrace_on_error
= node
->u
.vboolean
;
283 node
->type
= JS_BOOLEAN
;
284 node
->u
.vboolean
= vm
->stacktrace_on_error
;
287 /* ***************************************************************** */
288 else if (property
== ctx
->s_verbose
)
292 if (node
->type
!= JS_INTEGER
)
294 vm
->verbose
= node
->u
.vinteger
;
298 node
->type
= JS_INTEGER
;
299 node
->u
.vinteger
= vm
->verbose
;
302 /* ***************************************************************** */
303 else if (property
== ctx
->s_verboseStacktrace
)
307 if (node
->type
!= JS_BOOLEAN
)
309 vm
->verbose_stacktrace
= node
->u
.vboolean
;
313 node
->type
= JS_BOOLEAN
;
314 node
->u
.vboolean
= vm
->verbose_stacktrace
;
317 /* ***************************************************************** */
318 else if (property
== ctx
->s_version
)
323 js_vm_make_static_string (vm
, node
, VERSION
, strlen (VERSION
));
325 /* ***************************************************************** */
326 else if (property
== ctx
->s_versionMajor
)
331 node
->type
= JS_INTEGER
;
332 node
->u
.vinteger
= atoi (VERSION
);
334 /* ***************************************************************** */
335 else if (property
== ctx
->s_versionMinor
)
340 node
->type
= JS_INTEGER
;
341 node
->u
.vinteger
= atoi (VERSION
+ 2);
343 /* ***************************************************************** */
344 else if (property
== ctx
->s_versionPatch
)
349 node
->type
= JS_INTEGER
;
350 node
->u
.vinteger
= atoi (VERSION
+ 4);
352 /* ***************************************************************** */
353 else if (property
== ctx
->s_warnUndef
)
357 if (node
->type
!= JS_INTEGER
)
359 vm
->warn_undef
= node
->u
.vinteger
!= 0;
363 node
->type
= JS_INTEGER
;
364 node
->u
.vinteger
= vm
->warn_undef
;
367 /* ***************************************************************** */
371 node
->type
= JS_UNDEFINED
;
373 return JS_PROPERTY_UNKNOWN
;
376 return JS_PROPERTY_FOUND
;
384 sprintf (vm
->error
, "VM.%s: illegal value",
385 js_vm_symname (vm
, property
));
389 sprintf (vm
->error
, "VM.%s: immutable property",
390 js_vm_symname (vm
, property
));
403 js_builtin_VM (JSVirtualMachine
*vm
)
409 ctx
= js_calloc (vm
, 1, sizeof (*ctx
));
411 ctx
->s_garbageCollect
= js_vm_intern (vm
, "garbageCollect");
412 ctx
->s_stackTrace
= js_vm_intern (vm
, "stackTrace");
414 ctx
->s_dispatchMethod
= js_vm_intern (vm
, "dispatchMethod");
415 ctx
->s_fdCount
= js_vm_intern (vm
, "fdCount");
416 ctx
->s_gcCount
= js_vm_intern (vm
, "gcCount");
417 ctx
->s_gcTrigger
= js_vm_intern (vm
, "gcTrigger");
418 ctx
->s_heapAllocated
= js_vm_intern (vm
, "heapAllocated");
419 ctx
->s_heapFree
= js_vm_intern (vm
, "heapFree");
420 ctx
->s_heapSize
= js_vm_intern (vm
, "heapSize");
421 ctx
->s_numConstants
= js_vm_intern (vm
, "numConstants");
422 ctx
->s_numGlobals
= js_vm_intern (vm
, "numGlobals");
423 ctx
->s_stackSize
= js_vm_intern (vm
, "stackSize");
424 ctx
->s_stacktraceOnError
= js_vm_intern (vm
, "stacktraceOnError");
425 ctx
->s_verbose
= js_vm_intern (vm
, "verbose");
426 ctx
->s_verboseStacktrace
= js_vm_intern (vm
, "verboseStacktrace");
427 ctx
->s_version
= js_vm_intern (vm
, "version");
428 ctx
->s_versionMajor
= js_vm_intern (vm
, "versionMajor");
429 ctx
->s_versionMinor
= js_vm_intern (vm
, "versionMinor");
430 ctx
->s_versionPatch
= js_vm_intern (vm
, "versionPatch");
431 ctx
->s_warnUndef
= js_vm_intern (vm
, "warnUndef");
433 /* Object information. */
435 info
= js_vm_builtin_info_create (vm
);
437 info
->method_proc
= method
;
438 info
->property_proc
= property
;
439 info
->obj_context
= ctx
;
440 info
->obj_context_delete
= js_free
;
443 n
= &vm
->globals
[js_vm_intern (vm
, "VM")];
444 js_vm_builtin_create (vm
, n
, info
, NULL
);