8aa89de3f74aff903e71d4ba47061d38976dc745
2 * The builtin System 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/ksrc/b_system.c,v $
33 * print ([ANY...]) => undefined
35 * Properties: type mutable
38 * canonicalHost string
39 * canonicalHostCPU string
40 * canonicalHostVendor string
41 * canonicalHostOS string
43 * lineBreakSequence string
49 #include "ddk/ntddk.h"
54 * Types and definitions.
60 goto insecure_feature; \
69 method (JSVirtualMachine
*vm
, JSBuiltinInfo
*builtin_info
,
70 void *instance_context
, JSSymbol method
, JSNode
*result_return
,
73 SystemCtx
*ctx
= builtin_info
->obj_context
;
76 /* int secure_mode = vm->security & JS_VM_SECURE_SYSTEM; */
78 /* The default result. */
79 result_return
->type
= JS_UNDEFINED
;
81 if (method
== ctx
->s_mread
)
83 if (args
->u
.vinteger
!= 2)
85 if (args
[1].type
!= JS_INTEGER
)
86 goto argument_type_error
;
87 if (args
[2].type
!= JS_INTEGER
)
88 goto argument_type_error
;
90 if (args
[1].u
.vinteger
== 0) /* String type */
92 result_return
->type
= JS_STRING
;
93 js_vm_make_string (vm
, result_return
, (char *)args
[2].u
.vinteger
,
94 strlen ((char *)args
[2].u
.vinteger
));
96 else if( args
[1].u
.vinteger
== 1) /* Byte */
98 result_return
->type
= JS_INTEGER
;
99 result_return
->u
.vinteger
= *((BYTE
*)args
[2].u
.vinteger
);
101 else if( args
[1].u
.vinteger
== 2) /* Word */
103 result_return
->type
= JS_INTEGER
;
104 result_return
->u
.vinteger
= *((WORD
*)args
[2].u
.vinteger
);
106 else if( args
[1].u
.vinteger
== 4) /* Dword */
108 result_return
->type
= JS_INTEGER
;
109 result_return
->u
.vinteger
= *((DWORD
*)args
[2].u
.vinteger
);
112 else if (method
== ctx
->s_mwrite
)
114 if (args
->u
.vinteger
!= 3)
116 if (args
[1].type
!= JS_INTEGER
)
117 goto argument_type_error
;
118 if (args
[2].type
!= JS_INTEGER
)
119 goto argument_type_error
;
121 if (args
[1].u
.vinteger
== 0) /* String type */
123 if (args
[3].type
!= JS_STRING
)
124 goto argument_type_error
;
126 result_return
->type
= JS_BOOLEAN
;
127 result_return
->u
.vinteger
= 1;
128 cp
= js_string_to_c_string (vm
, &args
[3]);
129 strcpy((char *)args
[2].u
.vinteger
, cp
);
132 else if( args
[1].u
.vinteger
== 1) /* Byte */
134 result_return
->type
= JS_INTEGER
;
135 *((BYTE
*)args
[2].u
.vinteger
) = args
[3].u
.vinteger
;
137 else if( args
[1].u
.vinteger
== 2) /* Word */
139 result_return
->type
= JS_INTEGER
;
140 *((WORD
*)args
[2].u
.vinteger
) = args
[3].u
.vinteger
;
142 else if( args
[1].u
.vinteger
== 4) /* Dword */
144 result_return
->type
= JS_INTEGER
;
145 *((DWORD
*)args
[2].u
.vinteger
) = args
[3].u
.vinteger
;
148 /* ********************************************************************** */
149 else if (method
== ctx
->s_print
)
153 if (method
== ctx
->s_print
)
154 stream
= vm
->s_stdout
;
156 stream
= vm
->s_stderr
;
158 for (i
= 1; i
<= args
->u
.vinteger
; i
++)
162 js_vm_to_string (vm
, &args
[i
], &result
);
163 js_iostream_write (stream
, result
.u
.vstring
->data
,
164 result
.u
.vstring
->len
);
167 /* ********************************************************************** */
168 else if (method
== vm
->syms
.s_toString
)
170 if (args
->u
.vinteger
!= 0)
173 js_vm_make_static_string (vm
, result_return
, "System", 6);
175 /* ********************************************************************** */
177 JSSymbolList
*cur
= ctx
->registered_symbols
;
179 if( cur
->symbol
== method
&& cur
->registered_function
) {
180 return cur
->registered_function
181 (cur
->context
, result_return
, args
);
185 return JS_PROPERTY_UNKNOWN
;
188 return JS_PROPERTY_FOUND
;
196 sprintf (vm
->error
, "System.%s(): illegal amout of arguments",
197 js_vm_symname (vm
, method
));
201 sprintf (vm
->error
, "System.%s(): illegal argument",
202 js_vm_symname (vm
, method
));
205 /* insecure_feature: */
206 sprintf (vm
->error
, "System.%s(): not allowed in secure mode",
207 js_vm_symname (vm
, method
));
216 property (JSVirtualMachine
*vm
, JSBuiltinInfo
*builtin_info
,
217 void *instance_context
, JSSymbol property
, int set
, JSNode
*node
)
219 SystemCtx
*ctx
= builtin_info
->obj_context
;
221 if (property
== ctx
->s_bits
)
226 node
->type
= JS_INTEGER
;
228 node
->u
.vinteger
= 16;
229 #else /* not SIZEOF_INT == 2 */
232 node
->u
.vinteger
= 32;
233 #else /* not SIZEOF_LONG == 4 */
236 node
->u
.vinteger
= 64;
237 #else /* not SIZEOF_LONG == 8 */
240 node
->u
.vinteger
= 0;
242 #endif /* not SIZEOF_LONG == 8 */
243 #endif /* not SIZEOF_LONG == 4 */
244 #endif /* not SIZEOF_INT == 2 */
246 else if (property
== ctx
->s_canonicalHost
)
251 js_vm_make_static_string (vm
, node
, CANONICAL_HOST
,
252 strlen (CANONICAL_HOST
));
254 else if (property
== ctx
->s_canonicalHostCPU
)
259 js_vm_make_static_string (vm
, node
, CANONICAL_HOST_CPU
,
260 strlen (CANONICAL_HOST_CPU
));
262 else if (property
== ctx
->s_canonicalHostVendor
)
267 js_vm_make_static_string (vm
, node
, CANONICAL_HOST_VENDOR
,
268 strlen (CANONICAL_HOST_VENDOR
));
270 else if (property
== ctx
->s_canonicalHostOS
)
275 js_vm_make_static_string (vm
, node
, CANONICAL_HOST_OS
,
276 strlen (CANONICAL_HOST_OS
));
278 else if (property
== ctx
->s_lineBreakSequence
)
283 js_vm_make_static_string (vm
, node
, JS_HOST_LINE_BREAK
,
284 JS_HOST_LINE_BREAK_LEN
);
286 else if (property
== ctx
->s_stderr
)
291 JS_COPY (node
, &ctx
->pstderr
);
293 else if (property
== ctx
->s_stdin
)
298 JS_COPY (node
, &ctx
->pstdin
);
300 else if (property
== ctx
->s_stdout
)
305 JS_COPY (node
, &ctx
->pstdout
);
311 node
->type
= JS_UNDEFINED
;
313 return JS_PROPERTY_UNKNOWN
;
316 return JS_PROPERTY_FOUND
;
324 sprintf (vm
->error
, "System.%s: immutable property",
325 js_vm_symname (vm
, property
));
334 mark (JSBuiltinInfo
*builtin_info
, void *instance_context
)
336 SystemCtx
*ctx
= builtin_info
->obj_context
;
338 js_vm_mark (&ctx
->pstderr
);
339 js_vm_mark (&ctx
->pstdin
);
340 js_vm_mark (&ctx
->pstdout
);
349 js_builtin_System (PKJS kjs
) {
353 JSVirtualMachine
*vm
= kjs
->vm
;
355 kjs
->ctx
= (SystemCtx
*)js_calloc (vm
, 1, sizeof (*ctx
));
358 ctx
->s_print
= js_vm_intern (vm
, "print");
359 ctx
->s_mread
= js_vm_intern (vm
, "mread");
360 ctx
->s_mwrite
= js_vm_intern (vm
, "mwrite");
361 ctx
->s_reg
= js_vm_intern (vm
, "reg");
362 ctx
->s_regdir
= js_vm_intern (vm
, "regdir");
364 ctx
->s_bits
= js_vm_intern (vm
, "bits");
365 ctx
->s_canonicalHost
= js_vm_intern (vm
, "canonicalHost");
366 ctx
->s_canonicalHostCPU
= js_vm_intern (vm
, "canonicalHostCPU");
367 ctx
->s_canonicalHostVendor
= js_vm_intern (vm
, "canonicalHostVendor");
368 ctx
->s_canonicalHostOS
= js_vm_intern (vm
, "canonicalHostOS");
369 ctx
->s_errno
= js_vm_intern (vm
, "errno");
370 ctx
->s_lineBreakSequence
= js_vm_intern (vm
, "lineBreakSequence");
371 ctx
->s_stderr
= js_vm_intern (vm
, "stderr");
372 ctx
->s_stdin
= js_vm_intern (vm
, "stdin");
373 ctx
->s_stdout
= js_vm_intern (vm
, "stdout");
375 /* Object information. */
377 info
= js_vm_builtin_info_create (vm
);
379 info
->method_proc
= method
;
380 info
->property_proc
= property
;
381 info
->mark_proc
= mark
;
382 info
->obj_context
= ctx
;
383 info
->obj_context_delete
= js_free
;
386 n
= &vm
->globals
[js_vm_intern (vm
, "System")];
387 js_vm_builtin_create (vm
, n
, info
, NULL
);
389 /* Enter system properties. */
390 js_builtin_File_new (vm
, &ctx
->pstderr
, "stdout", vm
->s_stderr
, 1);
391 js_builtin_File_new (vm
, &ctx
->pstdin
, "stdin", vm
->s_stdin
, 1);
392 js_builtin_File_new (vm
, &ctx
->pstdout
, "stdout", vm
->s_stdout
, 1);