home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / libmocha / lm_hardw.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  4.1 KB  |  154 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /*
  20.  * JS reflection of the Hardware info top-level object.
  21.  *
  22.  * Tom Pixley, 1/2/98
  23.  */
  24. #include "lm.h"
  25.  
  26. typedef struct JSHardware {
  27.     MochaDecoder    *decoder;
  28. } JSHardware;
  29.  
  30. enum hardware_tinyid {
  31.     HARDWARE_ARCHITECTURE   = -1,
  32.     HARDWARE_CLOCK_SPEED    = -2,
  33.     HARDWARE_RAM        = -3
  34. };
  35.  
  36. static JSPropertySpec hardware_props[] = {
  37.     {"architecture",    HARDWARE_ARCHITECTURE,    JSPROP_ENUMERATE | JSPROP_READONLY},
  38.     {"clockSpeed",    HARDWARE_CLOCK_SPEED,    JSPROP_ENUMERATE | JSPROP_READONLY},
  39.     {"ram",        HARDWARE_RAM    ,    JSPROP_ENUMERATE | JSPROP_READONLY},
  40.     {0}
  41. };
  42.  
  43. extern JSClass lm_hardware_class;
  44.  
  45. PR_STATIC_CALLBACK(JSBool)
  46. hardware_getProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
  47. {
  48.     MWContext *context;
  49.     JSHardware *hardware;
  50.     jsint slot;
  51.     char * arch;
  52.  
  53.     if (!JSVAL_IS_INT(id))
  54.     return JS_TRUE;
  55.  
  56.     slot = JSVAL_TO_INT(id);
  57.  
  58.     hardware = JS_GetInstancePrivate(cx, obj, &lm_hardware_class, NULL);
  59.     if (!hardware)
  60.     return JS_TRUE;
  61.     context = hardware->decoder->window_context;
  62.     if (!context)
  63.     return JS_TRUE;
  64.  
  65.     switch (slot) {
  66.     case HARDWARE_ARCHITECTURE:
  67. #ifdef XP_WIN
  68.         arch = ET_moz_CallFunctionString((ETStringPtrFunc)FE_SystemCPUInfo, NULL);
  69.             *vp = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, arch));
  70.         XP_FREE(arch);
  71. #endif
  72.         break;
  73.     case HARDWARE_CLOCK_SPEED:
  74. #ifdef XP_WIN
  75.             *vp = INT_TO_JSVAL(
  76.         ET_moz_CallFunctionInt((ETIntPtrFunc)FE_SystemClockSpeed, NULL));
  77. #endif
  78.         break;
  79.     case HARDWARE_RAM:
  80. #ifdef XP_WIN
  81.             *vp = INT_TO_JSVAL(
  82.         ET_moz_CallFunctionInt((ETIntPtrFunc)FE_SystemRAM, NULL));
  83. #endif
  84.         break;
  85.     default:
  86.         return JS_TRUE;
  87.     }
  88.     return JS_TRUE;
  89. }
  90.  
  91.  
  92. PR_STATIC_CALLBACK(void)
  93. hardware_finalize(JSContext *cx, JSObject *obj)
  94. {
  95.     JSHardware *hardware;
  96.  
  97.     hardware = JS_GetPrivate(cx, obj);
  98.     if (!hardware)
  99.     return;
  100.     DROP_BACK_COUNT(hardware->decoder);
  101.     JS_free(cx, hardware);
  102. }
  103.  
  104. JSClass lm_hardware_class = {
  105.     "Hardware", JSCLASS_HAS_PRIVATE,
  106.     JS_PropertyStub, JS_PropertyStub, hardware_getProperty, hardware_getProperty,
  107.     JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, hardware_finalize
  108. };
  109.  
  110. PR_STATIC_CALLBACK(JSBool)
  111. Hardware(JSContext *cx, JSObject *obj,
  112.           uint argc, jsval *argv, jsval *rval)
  113. {
  114.     return JS_TRUE;
  115. }
  116.  
  117. JSObject *
  118. lm_DefineHardware(MochaDecoder *decoder, JSObject *parent)
  119. {
  120.     JSObject *obj;
  121.     JSContext *cx;
  122.     JSHardware *hardware;
  123.  
  124.     if (parent == decoder->window_object) {
  125.         obj = decoder->hardware;
  126.         if (obj)
  127.             return obj;
  128.     }
  129.  
  130.     cx = decoder->js_context;
  131.     if (!(hardware = JS_malloc(cx, sizeof(JSHardware))))
  132.     return 0;
  133.     hardware->decoder = NULL; /* in case of error below */
  134.  
  135.     obj = JS_InitClass(cx, decoder->window_object, NULL, &lm_hardware_class,
  136.                        Hardware, 0, hardware_props, NULL, NULL, NULL);
  137.  
  138.     if (!obj || !JS_SetPrivate(cx, obj, hardware)) {
  139.         JS_free(cx, hardware);
  140.         return 0;
  141.     }
  142.     
  143.     if (!JS_DefineProperty(cx, parent, "hardware",
  144.                OBJECT_TO_JSVAL(obj), NULL, NULL,
  145.                            JSPROP_ENUMERATE | JSPROP_READONLY)) {
  146.         return 0;
  147.     }
  148.  
  149.     hardware->decoder = HOLD_BACK_COUNT(decoder);
  150.     if (parent == decoder->window_object)
  151.         decoder->hardware = obj;
  152.     return obj;
  153. }
  154.