Added separate number class and initialization.

This commit is contained in:
rogerl%netscape.com 2002-10-25 17:58:43 +00:00
parent 13b370c337
commit c6a9a760c5
6 changed files with 161 additions and 47 deletions

View File

@ -2605,27 +2605,6 @@ doUnary:
return BOOLEAN_TO_JS2VAL(JSDOUBLE_IS_NaN(d));
}
static js2val Number_Constructor(JS2Metadata *meta, const js2val thisValue, js2val argv[], uint32 argc)
{
js2val thatValue = OBJECT_TO_JS2VAL(new NumberInstance(meta->numberClass));
NumberInstance *numInst = checked_cast<NumberInstance *>(JS2VAL_TO_OBJECT(thatValue));
if (argc > 0)
numInst->mValue = meta->toFloat64(argv[0]);
else
numInst->mValue = 0.0;
return thatValue;
}
static js2val Number_toString(JS2Metadata *meta, const js2val thisValue, js2val * /*argv*/, uint32 /*argc*/)
{
if (meta->objectType(thisValue) != meta->numberClass)
meta->reportError(Exception::typeError, "Number.toString called on something other than a number thing", meta->engine->errorPos());
NumberInstance *numInst = checked_cast<NumberInstance *>(JS2VAL_TO_OBJECT(thisValue));
return meta->engine->allocString(numberToString(&numInst->mValue));
}
void JS2Metadata::addGlobalObjectFunction(char *name, NativeCode *code)
{
FixedInstance *fInst = new FixedInstance(functionClass);
@ -2685,11 +2664,11 @@ doUnary:
fInst->fWrap = new FunctionWrapper(true, new ParameterFrame(JS2VAL_VOID, true), Object_toString);
writeDynamicProperty(objectClass->prototype, new Multiname(engine->toString_StringAtom, publicNamespace), true, OBJECT_TO_JS2VAL(fInst), RunPhase);
// needed for class instance variables...
// needed for class instance variables etc...
NamespaceList publicNamespaceList;
publicNamespaceList.push_back(publicNamespace);
Variable *v;
InstanceMember *m;
/*** ECMA 3 Date Class ***/
MAKEBUILTINCLASS(dateClass, objectClass, true, true, true, &world.identifiers["Date"]);
@ -2702,17 +2681,7 @@ doUnary:
MAKEBUILTINCLASS(regexpClass, objectClass, true, true, true, &world.identifiers["RegExp"]);
v = new Variable(classClass, OBJECT_TO_JS2VAL(regexpClass), true);
defineStaticMember(&env, &world.identifiers["RegExp"], &publicNamespaceList, Attribute::NoOverride, false, ReadWriteAccess, v, 0);
regexpClass->construct = RegExp_Constructor;
m = new InstanceVariable(objectClass, false, false, regexpClass->slotCount++);
defineInstanceMember(regexpClass, &cxt, &world.identifiers["source"], &publicNamespaceList, Attribute::NoOverride, false, ReadWriteAccess, m, 0);
m = new InstanceVariable(objectClass, false, false, regexpClass->slotCount++);
defineInstanceMember(regexpClass, &cxt, &world.identifiers["global"], &publicNamespaceList, Attribute::NoOverride, false, ReadWriteAccess, m, 0);
m = new InstanceVariable(objectClass, false, false, regexpClass->slotCount++);
defineInstanceMember(regexpClass, &cxt, &world.identifiers["lastIndex"], &publicNamespaceList, Attribute::NoOverride, false, ReadWriteAccess, m, 0);
m = new InstanceVariable(objectClass, false, false, regexpClass->slotCount++);
defineInstanceMember(regexpClass, &cxt, &world.identifiers["ignoreCase"], &publicNamespaceList, Attribute::NoOverride, false, ReadWriteAccess, m, 0);
m = new InstanceVariable(objectClass, false, false, regexpClass->slotCount++);
defineInstanceMember(regexpClass, &cxt, &world.identifiers["multiline"], &publicNamespaceList, Attribute::NoOverride, false, ReadWriteAccess, m, 0);
initRegExpObject(this);
/*** ECMA 3 String Class ***/
v = new Variable(classClass, OBJECT_TO_JS2VAL(stringClass), true);
@ -2722,12 +2691,7 @@ doUnary:
/*** ECMA 3 Number Class ***/
v = new Variable(classClass, OBJECT_TO_JS2VAL(numberClass), true);
defineStaticMember(&env, &world.identifiers["Number"], &publicNamespaceList, Attribute::NoOverride, false, ReadWriteAccess, v, 0);
numberClass->construct = Number_Constructor;
fInst = new FixedInstance(functionClass);
fInst->fWrap = new FunctionWrapper(true, new ParameterFrame(JS2VAL_VOID, true), Number_toString);
m = new InstanceMethod(fInst);
defineInstanceMember(numberClass, &cxt, &world.identifiers["toString"], &publicNamespaceList, Attribute::NoOverride, false, ReadWriteAccess, m, 0);
initNumberObject(this);
/*** ECMA 3 Math Class ***/
MAKEBUILTINCLASS(mathClass, objectClass, true, true, true, &world.identifiers["Math"]);

View File

@ -61,6 +61,8 @@ extern void initDateObject(JS2Metadata *meta);
extern void initStringObject(JS2Metadata *meta);
extern void initMathObject(JS2Metadata *meta);
extern void initArrayObject(JS2Metadata *meta);
extern void initRegExpObject(JS2Metadata *meta);
extern void initNumberObject(JS2Metadata *meta);
extern js2val String_Constructor(JS2Metadata *meta, const js2val thisValue, js2val *argv, uint32 argc);
extern js2val RegExp_Constructor(JS2Metadata *meta, const js2val thisValue, js2val *argv, uint32 argc);

119
js2/src/js2number.cpp Normal file
View File

@ -0,0 +1,119 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is the JavaScript 2 Prototype.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the
* terms of the GNU Public License (the "GPL"), in which case the
* provisions of the GPL are applicable instead of those above.
* If you wish to allow use of your version of this file only
* under the terms of the GPL and not to allow others to use your
* version of this file under the NPL, indicate your decision by
* deleting the provisions above and replace them with the notice
* and other provisions required by the GPL. If you do not delete
* the provisions above, a recipient may use your version of this
* file under either the NPL or the GPL.
*/
#ifdef _WIN32
// Turn off warnings about identifiers too long in browser information
#pragma warning(disable: 4786)
#pragma warning(disable: 4711)
#pragma warning(disable: 4710)
#endif
#include <algorithm>
#include <list>
#include <map>
#include <stack>
#include "world.h"
#include "utilities.h"
#include "js2value.h"
#include "numerics.h"
#include "reader.h"
#include "parser.h"
#include "regexp.h"
#include "js2engine.h"
#include "bytecodecontainer.h"
#include "js2metadata.h"
namespace JavaScript {
namespace MetaData {
static js2val Number_Constructor(JS2Metadata *meta, const js2val thisValue, js2val argv[], uint32 argc)
{
js2val thatValue = OBJECT_TO_JS2VAL(new NumberInstance(meta->numberClass));
NumberInstance *numInst = checked_cast<NumberInstance *>(JS2VAL_TO_OBJECT(thatValue));
if (argc > 0)
numInst->mValue = meta->toFloat64(argv[0]);
else
numInst->mValue = 0.0;
return thatValue;
}
static js2val Number_toString(JS2Metadata *meta, const js2val thisValue, js2val * /*argv*/, uint32 /*argc*/)
{
if (meta->objectType(thisValue) != meta->numberClass)
meta->reportError(Exception::typeError, "Number.toString called on something other than a number thing", meta->engine->errorPos());
NumberInstance *numInst = checked_cast<NumberInstance *>(JS2VAL_TO_OBJECT(thisValue));
return meta->engine->allocString(numberToString(&numInst->mValue));
}
#define MAKE_INSTANCE_VARIABLE(name, type) \
m = new InstanceVariable(type, false, false, meta->numberClass->slotCount++); \
meta->defineInstanceMember(meta->numberClass, &meta->cxt, &meta->world.identifiers[name], &publicNamespaceList, Attribute::NoOverride, false, ReadWriteAccess, m, 0);
void initNumberObject(JS2Metadata *meta)
{
meta->numberClass->construct = Number_Constructor;
#define N_CONSTANTS_COUNT (5)
struct {
char *name;
float64 value;
} NumberObjectConstants[N_CONSTANTS_COUNT] = {
{ "MAX_VALUE", maxDouble },
{ "MIN_VALUE", minDouble },
{ "NaN", nan },
{ "POSITIVE_INFINITY", positiveInfinity },
{ "NEGATIVE_INFINITY", negativeInfinity },
};
NamespaceList publicNamespaceList;
publicNamespaceList.push_back(meta->publicNamespace);
uint32 i;
meta->env.addFrame(meta->numberClass);
for (i = 0; i < N_CONSTANTS_COUNT; i++)
{
Variable *v = new Variable(meta->numberClass, meta->engine->allocNumber(NumberObjectConstants[i].value), true);
meta->defineStaticMember(&meta->env, &meta->world.identifiers[NumberObjectConstants[i].name], &publicNamespaceList, Attribute::NoOverride, false, ReadWriteAccess, v, 0);
}
meta->env.removeTopFrame();
}
}
}

View File

@ -230,5 +230,34 @@ namespace MetaData {
return thatValue;
}
void initRegExpObject(JS2Metadata *meta)
{
meta->regexpClass->construct = RegExp_Constructor;
NamespaceList publicNamespaceList;
publicNamespaceList.push_back(meta->publicNamespace);
#define INSTANCE_VAR_COUNT (5)
struct {
char *name;
JS2Class *type;
} RegExpInstanceVars[INSTANCE_VAR_COUNT] = {
{ "source", meta->stringClass },
{ "global", meta->booleanClass },
{ "multiline", meta->booleanClass },
{ "ignoreCase", meta->booleanClass },
{ "lastIndex", meta->numberClass },
};
for (uint32 i = 0; i < INSTANCE_VAR_COUNT; i++)
{
InstanceMember *m = new InstanceVariable(RegExpInstanceVars[i].type, false, false, meta->regexpClass->slotCount++);
meta->defineInstanceMember(meta->regexpClass, &meta->cxt, &meta->world.identifiers[RegExpInstanceVars[i].name], &publicNamespaceList, Attribute::NoOverride, false, ReadWriteAccess, m, 0);
}
}
}
}

View File

@ -232,8 +232,8 @@ static PRLock *freelist_lock;
double JS::positiveInfinity;
double JS::negativeInfinity;
double JS::nan;
double JS::minValue;
double JS::maxValue;
double JS::minDouble;
double JS::maxDouble;
@ -248,9 +248,9 @@ InitNumerics::InitNumerics()
word1(JS::negativeInfinity) = 0;
word0(JS::nan) = 0x7FFFFFFF;
word1(JS::nan) = 0xFFFFFFFF;
word0(JS::minValue) = 0;
word1(JS::minValue) = 1;
JS::maxValue = 1.7976931348623157E+308;
word0(JS::minDouble) = 0;
word1(JS::minDouble) = 1;
JS::maxDouble = 1.7976931348623157E+308;
}
// had to move these here since they depend upon the values

View File

@ -66,8 +66,8 @@ namespace JavaScript {
extern double positiveInfinity;
extern double negativeInfinity;
extern double nan;
extern double minValue;
extern double maxValue;
extern double minDouble;
extern double maxDouble;
static const double two32minus1 = 4294967295.0;
static const double two32 = 4294967296.0;