8aa89de3f74aff903e71d4ba47061d38976dc745
[reactos.git] / reactos / lib / kjs / ksrc / b_system.c
1 /*
2 * The builtin System object.
3 * Copyright (c) 1998-1999 New Generation Software (NGS) Oy
4 *
5 * Author: Markku Rossi <mtr@ngs.fi>
6 */
7
8 /*
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.
13 *
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.
18 *
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,
22 * MA 02111-1307, USA
23 */
24
25 /*
26 * $Source: /cygdrive/c/RCVS/CVS/ReactOS/reactos/lib/kjs/ksrc/b_system.c,v $
27 * $Id$
28 */
29
30 /*
31 * Static methods:
32 *
33 * print ([ANY...]) => undefined
34 *
35 * Properties: type mutable
36 *
37 * bits integer
38 * canonicalHost string
39 * canonicalHostCPU string
40 * canonicalHostVendor string
41 * canonicalHostOS string
42 * errno integer
43 * lineBreakSequence string
44 * stderr file
45 * stdin file
46 * stdout file
47 */
48
49 #include "ddk/ntddk.h"
50 #include "jsint.h"
51 #include "kjs.h"
52
53 /*
54 * Types and definitions.
55 */
56
57 #define INSECURE() \
58 do { \
59 if (secure_mode) \
60 goto insecure_feature; \
61 } while (0)
62
63 /*
64 * Static functions.
65 */
66
67 /* Method proc */
68 static int
69 method (JSVirtualMachine *vm, JSBuiltinInfo *builtin_info,
70 void *instance_context, JSSymbol method, JSNode *result_return,
71 JSNode *args)
72 {
73 SystemCtx *ctx = builtin_info->obj_context;
74 int i;
75 char *cp;
76 /* int secure_mode = vm->security & JS_VM_SECURE_SYSTEM; */
77
78 /* The default result. */
79 result_return->type = JS_UNDEFINED;
80
81 if (method == ctx->s_mread)
82 {
83 if (args->u.vinteger != 2)
84 goto argument_error;
85 if (args[1].type != JS_INTEGER)
86 goto argument_type_error;
87 if (args[2].type != JS_INTEGER)
88 goto argument_type_error;
89
90 if (args[1].u.vinteger == 0) /* String type */
91 {
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));
95 }
96 else if( args[1].u.vinteger == 1) /* Byte */
97 {
98 result_return->type = JS_INTEGER;
99 result_return->u.vinteger = *((BYTE *)args[2].u.vinteger);
100 }
101 else if( args[1].u.vinteger == 2) /* Word */
102 {
103 result_return->type = JS_INTEGER;
104 result_return->u.vinteger = *((WORD *)args[2].u.vinteger);
105 }
106 else if( args[1].u.vinteger == 4) /* Dword */
107 {
108 result_return->type = JS_INTEGER;
109 result_return->u.vinteger = *((DWORD *)args[2].u.vinteger);
110 }
111 }
112 else if (method == ctx->s_mwrite)
113 {
114 if (args->u.vinteger != 3)
115 goto argument_error;
116 if (args[1].type != JS_INTEGER)
117 goto argument_type_error;
118 if (args[2].type != JS_INTEGER)
119 goto argument_type_error;
120
121 if (args[1].u.vinteger == 0) /* String type */
122 {
123 if (args[3].type != JS_STRING)
124 goto argument_type_error;
125
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);
130 js_free(cp);
131 }
132 else if( args[1].u.vinteger == 1) /* Byte */
133 {
134 result_return->type = JS_INTEGER;
135 *((BYTE *)args[2].u.vinteger) = args[3].u.vinteger;
136 }
137 else if( args[1].u.vinteger == 2) /* Word */
138 {
139 result_return->type = JS_INTEGER;
140 *((WORD *)args[2].u.vinteger) = args[3].u.vinteger;
141 }
142 else if( args[1].u.vinteger == 4) /* Dword */
143 {
144 result_return->type = JS_INTEGER;
145 *((DWORD *)args[2].u.vinteger) = args[3].u.vinteger;
146 }
147 }
148 /* ********************************************************************** */
149 else if (method == ctx->s_print)
150 {
151 JSIOStream *stream;
152
153 if (method == ctx->s_print)
154 stream = vm->s_stdout;
155 else
156 stream = vm->s_stderr;
157
158 for (i = 1; i <= args->u.vinteger; i++)
159 {
160 JSNode result;
161
162 js_vm_to_string (vm, &args[i], &result);
163 js_iostream_write (stream, result.u.vstring->data,
164 result.u.vstring->len);
165 }
166 }
167 /* ********************************************************************** */
168 else if (method == vm->syms.s_toString)
169 {
170 if (args->u.vinteger != 0)
171 goto argument_error;
172
173 js_vm_make_static_string (vm, result_return, "System", 6);
174 }
175 /* ********************************************************************** */
176 else {
177 JSSymbolList *cur = ctx->registered_symbols;
178 while( cur ) {
179 if( cur->symbol == method && cur->registered_function ) {
180 return cur->registered_function
181 (cur->context, result_return, args);
182 }
183 cur = cur->next;
184 }
185 return JS_PROPERTY_UNKNOWN;
186 }
187
188 return JS_PROPERTY_FOUND;
189
190
191 /*
192 * Error handling.
193 */
194
195 argument_error:
196 sprintf (vm->error, "System.%s(): illegal amout of arguments",
197 js_vm_symname (vm, method));
198 js_vm_error (vm);
199
200 argument_type_error:
201 sprintf (vm->error, "System.%s(): illegal argument",
202 js_vm_symname (vm, method));
203 js_vm_error (vm);
204
205 /* insecure_feature: */
206 sprintf (vm->error, "System.%s(): not allowed in secure mode",
207 js_vm_symname (vm, method));
208 js_vm_error (vm);
209
210 /* NOTREACHED */
211 return 0;
212 }
213
214 /* Property proc. */
215 static int
216 property (JSVirtualMachine *vm, JSBuiltinInfo *builtin_info,
217 void *instance_context, JSSymbol property, int set, JSNode *node)
218 {
219 SystemCtx *ctx = builtin_info->obj_context;
220
221 if (property == ctx->s_bits)
222 {
223 if (set)
224 goto immutable;
225
226 node->type = JS_INTEGER;
227 #if SIZEOF_INT == 2
228 node->u.vinteger = 16;
229 #else /* not SIZEOF_INT == 2 */
230
231 #if SIZEOF_LONG == 4
232 node->u.vinteger = 32;
233 #else /* not SIZEOF_LONG == 4 */
234
235 #if SIZEOF_LONG == 8
236 node->u.vinteger = 64;
237 #else /* not SIZEOF_LONG == 8 */
238
239 /* Do not know. */
240 node->u.vinteger = 0;
241
242 #endif /* not SIZEOF_LONG == 8 */
243 #endif /* not SIZEOF_LONG == 4 */
244 #endif /* not SIZEOF_INT == 2 */
245 }
246 else if (property == ctx->s_canonicalHost)
247 {
248 if (set)
249 goto immutable;
250
251 js_vm_make_static_string (vm, node, CANONICAL_HOST,
252 strlen (CANONICAL_HOST));
253 }
254 else if (property == ctx->s_canonicalHostCPU)
255 {
256 if (set)
257 goto immutable;
258
259 js_vm_make_static_string (vm, node, CANONICAL_HOST_CPU,
260 strlen (CANONICAL_HOST_CPU));
261 }
262 else if (property == ctx->s_canonicalHostVendor)
263 {
264 if (set)
265 goto immutable;
266
267 js_vm_make_static_string (vm, node, CANONICAL_HOST_VENDOR,
268 strlen (CANONICAL_HOST_VENDOR));
269 }
270 else if (property == ctx->s_canonicalHostOS)
271 {
272 if (set)
273 goto immutable;
274
275 js_vm_make_static_string (vm, node, CANONICAL_HOST_OS,
276 strlen (CANONICAL_HOST_OS));
277 }
278 else if (property == ctx->s_lineBreakSequence)
279 {
280 if (set)
281 goto immutable;
282
283 js_vm_make_static_string (vm, node, JS_HOST_LINE_BREAK,
284 JS_HOST_LINE_BREAK_LEN);
285 }
286 else if (property == ctx->s_stderr)
287 {
288 if (set)
289 goto immutable;
290
291 JS_COPY (node, &ctx->pstderr);
292 }
293 else if (property == ctx->s_stdin)
294 {
295 if (set)
296 goto immutable;
297
298 JS_COPY (node, &ctx->pstdin);
299 }
300 else if (property == ctx->s_stdout)
301 {
302 if (set)
303 goto immutable;
304
305 JS_COPY (node, &ctx->pstdout);
306 }
307
308 else
309 {
310 if (!set)
311 node->type = JS_UNDEFINED;
312
313 return JS_PROPERTY_UNKNOWN;
314 }
315
316 return JS_PROPERTY_FOUND;
317
318
319 /*
320 * Error handling.
321 */
322
323 immutable:
324 sprintf (vm->error, "System.%s: immutable property",
325 js_vm_symname (vm, property));
326 js_vm_error (vm);
327
328 /* NOTREACHED. */
329 return 0;
330 }
331
332 /* Mark proc. */
333 static void
334 mark (JSBuiltinInfo *builtin_info, void *instance_context)
335 {
336 SystemCtx *ctx = builtin_info->obj_context;
337
338 js_vm_mark (&ctx->pstderr);
339 js_vm_mark (&ctx->pstdin);
340 js_vm_mark (&ctx->pstdout);
341 }
342
343
344 /*
345 * Global functions.
346 */
347
348 void
349 js_builtin_System (PKJS kjs) {
350 JSNode *n;
351 JSBuiltinInfo *info;
352 SystemCtx *ctx;
353 JSVirtualMachine *vm = kjs->vm;
354
355 kjs->ctx = (SystemCtx *)js_calloc (vm, 1, sizeof (*ctx));
356 ctx = kjs->ctx;
357
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");
363
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");
374
375 /* Object information. */
376
377 info = js_vm_builtin_info_create (vm);
378
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;
384
385 /* Define it. */
386 n = &vm->globals[js_vm_intern (vm, "System")];
387 js_vm_builtin_create (vm, n, info, NULL);
388
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);
393 }