2009-04-18 19:00:33 +02:00
|
|
|
/* Copyright (C) 2009 Wildfire Games.
|
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2004-10-07 21:23:35 +02:00
|
|
|
#include "precompiled.h"
|
2008-07-04 13:10:18 +02:00
|
|
|
|
2004-10-07 21:23:35 +02:00
|
|
|
#include "JSConversions.h"
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "simulation/Entity.h"
|
|
|
|
#include "graphics/ObjectManager.h"
|
|
|
|
#include "maths/scripting/JSInterface_Vector3D.h"
|
|
|
|
#include "ps/Parser.h"
|
2006-06-09 18:44:16 +02:00
|
|
|
#include "ps/Player.h"
|
2006-07-13 05:29:33 +02:00
|
|
|
#include "simulation/EntityTemplate.h"
|
2006-07-26 16:04:52 +02:00
|
|
|
#include "lib/sysdep/sysdep.h" // isfinite
|
2006-08-19 22:24:49 +02:00
|
|
|
#include <math.h>
|
2006-04-19 07:30:02 +02:00
|
|
|
#include <cfloat>
|
2009-07-14 17:11:05 +02:00
|
|
|
#include "scripting/ScriptableComplex.inl"
|
2006-04-19 07:30:02 +02:00
|
|
|
|
2004-10-07 21:23:35 +02:00
|
|
|
// HEntity
|
|
|
|
|
|
|
|
template<> HEntity* ToNative<HEntity>( JSContext* cx, JSObject* obj )
|
|
|
|
{
|
|
|
|
CEntity* e = ToNative<CEntity>( cx, obj );
|
2005-05-10 09:13:25 +02:00
|
|
|
return( e ? &( e->me ) : NULL );
|
2004-10-07 21:23:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<> JSObject* ToScript<HEntity>( HEntity* Native )
|
|
|
|
{
|
|
|
|
return( ToScript<CEntity>( &( **Native ) ) );
|
|
|
|
}
|
|
|
|
|
2005-01-18 01:46:18 +01:00
|
|
|
// CPlayer*
|
|
|
|
template<> bool ToPrimitive<CPlayer*>( JSContext* cx, jsval v, CPlayer*& Storage )
|
|
|
|
{
|
2005-05-10 09:13:25 +02:00
|
|
|
if( !JSVAL_IS_OBJECT( v ) || ( v == JSVAL_NULL ) ) return( false );
|
2005-01-18 01:46:18 +01:00
|
|
|
CPlayer* Data = (CPlayer*)JS_GetInstancePrivate( cx, JSVAL_TO_OBJECT( v ), &CPlayer::JSI_class, NULL );
|
|
|
|
if( !Data ) return( false );
|
|
|
|
Storage = Data;
|
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> JSObject* ToScript<CPlayer*>( CPlayer** Native )
|
|
|
|
{
|
|
|
|
return( ToScript<CPlayer>( *Native ) );
|
|
|
|
}
|
|
|
|
|
2006-07-13 05:29:33 +02:00
|
|
|
// CEntityTemplate*
|
2004-10-07 21:23:35 +02:00
|
|
|
|
2006-07-13 05:29:33 +02:00
|
|
|
template<> bool ToPrimitive<CEntityTemplate*>( JSContext* cx, jsval v, CEntityTemplate*& Storage )
|
2004-10-07 21:23:35 +02:00
|
|
|
{
|
2005-05-10 09:13:25 +02:00
|
|
|
if( !JSVAL_IS_OBJECT( v ) || ( v == JSVAL_NULL ) ) return( false );
|
2006-07-13 05:29:33 +02:00
|
|
|
CEntityTemplate* Data = (CEntityTemplate*)JS_GetInstancePrivate( cx, JSVAL_TO_OBJECT( v ), &CEntityTemplate::JSI_class, NULL );
|
2004-10-07 21:23:35 +02:00
|
|
|
if( !Data ) return( false );
|
|
|
|
Storage = Data;
|
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
|
2006-07-13 05:29:33 +02:00
|
|
|
template<> JSObject* ToScript<CEntityTemplate*>( CEntityTemplate** Native )
|
2004-10-07 21:23:35 +02:00
|
|
|
{
|
2006-07-13 05:29:33 +02:00
|
|
|
return( ToScript<CEntityTemplate>( *Native ) );
|
2004-10-07 21:23:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CVector3D
|
|
|
|
|
|
|
|
template<> CVector3D* ToNative<CVector3D>( JSContext* cx, JSObject* obj )
|
|
|
|
{
|
2005-05-10 09:13:25 +02:00
|
|
|
JSI_Vector3D::Vector3D_Info* v = (JSI_Vector3D::Vector3D_Info*)JS_GetInstancePrivate( cx, obj, &JSI_Vector3D::JSI_class, NULL );
|
|
|
|
return( v ? v->vector : NULL );
|
2004-10-07 21:23:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<> JSObject* ToScript<CVector3D>( CVector3D* Native )
|
|
|
|
{
|
|
|
|
JSObject* Script = JS_NewObject( g_ScriptingHost.GetContext(), &JSI_Vector3D::JSI_class, NULL, NULL );
|
|
|
|
JS_SetPrivate( g_ScriptingHost.GetContext(), Script, new JSI_Vector3D::Vector3D_Info( *Native ) );
|
|
|
|
return( Script );
|
|
|
|
}
|
|
|
|
|
2005-03-29 00:13:47 +02:00
|
|
|
template<> jsval ToJSVal<CVector3D>( const CVector3D& Native )
|
|
|
|
{
|
|
|
|
JSObject* Script = JS_NewObject( g_ScriptingHost.GetContext(), &JSI_Vector3D::JSI_class, NULL, NULL );
|
|
|
|
JS_SetPrivate( g_ScriptingHost.GetContext(), Script, new JSI_Vector3D::Vector3D_Info( Native ) );
|
|
|
|
return( OBJECT_TO_JSVAL( Script ) );
|
|
|
|
}
|
2004-10-07 21:23:35 +02:00
|
|
|
|
|
|
|
// CScriptObject
|
|
|
|
|
|
|
|
template<> jsval ToJSVal<CScriptObject>( CScriptObject& Native )
|
|
|
|
{
|
2004-10-23 16:39:28 +02:00
|
|
|
return( OBJECT_TO_JSVAL( Native.GetFunctionObject() ) );
|
2004-10-07 21:23:35 +02:00
|
|
|
}
|
|
|
|
|
2005-08-09 17:55:44 +02:00
|
|
|
template<> bool ToPrimitive<CScriptObject>( JSContext* UNUSED(cx), jsval v, CScriptObject& Storage )
|
2004-10-07 21:23:35 +02:00
|
|
|
{
|
|
|
|
Storage.SetJSVal( v );
|
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
|
2004-11-11 08:09:32 +01:00
|
|
|
// int
|
2004-10-07 21:23:35 +02:00
|
|
|
|
2004-11-11 08:09:32 +01:00
|
|
|
template<> jsval ToJSVal<int>( const int& Native )
|
2004-10-07 21:23:35 +02:00
|
|
|
{
|
|
|
|
return( INT_TO_JSVAL( Native ) );
|
|
|
|
}
|
|
|
|
|
2004-11-11 08:09:32 +01:00
|
|
|
template<> jsval ToJSVal<int>( int& Native )
|
2004-10-07 21:23:35 +02:00
|
|
|
{
|
|
|
|
return( INT_TO_JSVAL( Native ) );
|
|
|
|
}
|
|
|
|
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
template<> bool ToPrimitive<int>( JSContext* cx, jsval v, int& Storage )
|
|
|
|
{
|
|
|
|
JSBool ok = JS_ValueToInt32(cx, v, (int32*)&Storage);
|
|
|
|
return ok == JS_TRUE;
|
|
|
|
}
|
2005-08-09 17:55:44 +02:00
|
|
|
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
// unsigned
|
2005-08-09 17:55:44 +02:00
|
|
|
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
template<> jsval ToJSVal<unsigned>( const unsigned& Native )
|
|
|
|
{
|
|
|
|
return( INT_TO_JSVAL( Native ) );
|
|
|
|
}
|
2005-08-09 17:55:44 +02:00
|
|
|
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
template<> jsval ToJSVal<unsigned>( unsigned& Native )
|
|
|
|
{
|
|
|
|
return( INT_TO_JSVAL( Native ) );
|
|
|
|
}
|
2005-08-09 17:55:44 +02:00
|
|
|
|
2008-07-04 13:10:18 +02:00
|
|
|
template<> bool ToPrimitive<unsigned>( JSContext* cx, jsval v, unsigned& Storage )
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
{
|
|
|
|
int temp;
|
|
|
|
if(!ToPrimitive<int>(cx, v, temp))
|
|
|
|
return false;
|
|
|
|
if(temp < 0)
|
|
|
|
return false;
|
|
|
|
Storage = (unsigned)temp;
|
|
|
|
return true;
|
|
|
|
}
|
2005-08-09 17:55:44 +02:00
|
|
|
|
2008-07-04 13:10:18 +02:00
|
|
|
// long
|
|
|
|
template<> jsval ToJSVal<long>( const long& Native )
|
|
|
|
{
|
|
|
|
return( INT_TO_JSVAL( (int)Native ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> jsval ToJSVal<long>( long& Native )
|
|
|
|
{
|
|
|
|
return( INT_TO_JSVAL( (int)Native ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> bool ToPrimitive<long>( JSContext* cx, jsval v, long& Storage )
|
|
|
|
{
|
|
|
|
int32 tmp;
|
|
|
|
JSBool ok = JS_ValueToInt32(cx, v, &tmp);
|
|
|
|
Storage = (long)tmp;
|
|
|
|
return ok == JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// unsigned long
|
|
|
|
template<> jsval ToJSVal<unsigned long>( const unsigned long& Native )
|
|
|
|
{
|
|
|
|
return( INT_TO_JSVAL( (int)Native ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> jsval ToJSVal<unsigned long>( unsigned long& Native )
|
|
|
|
{
|
|
|
|
return( INT_TO_JSVAL( (int)Native ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> bool ToPrimitive<unsigned long>( JSContext* cx, jsval v, unsigned long& Storage )
|
|
|
|
{
|
|
|
|
int32 tmp;
|
|
|
|
JSBool ok = JS_ValueToInt32(cx, v, &tmp);
|
|
|
|
Storage = (unsigned long)tmp;
|
|
|
|
return ok == JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2008-06-29 23:24:46 +02:00
|
|
|
// see comment at declaration of specialization
|
|
|
|
#if !GCC_VERSION
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
#if ARCH_AMD64
|
|
|
|
|
|
|
|
template<> jsval ToJSVal<size_t>( const size_t& Native )
|
2004-11-11 08:09:32 +01:00
|
|
|
{
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
return( INT_TO_JSVAL( (int)Native ) );
|
2004-11-11 08:09:32 +01:00
|
|
|
}
|
|
|
|
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
template<> jsval ToJSVal<size_t>( size_t& Native )
|
2004-11-11 08:09:32 +01:00
|
|
|
{
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
return( INT_TO_JSVAL( (int)Native ) );
|
2004-11-11 08:09:32 +01:00
|
|
|
}
|
|
|
|
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
template<> bool ToPrimitive<size_t>( JSContext* cx, jsval v, size_t& Storage )
|
2004-11-11 08:09:32 +01:00
|
|
|
{
|
2005-08-09 17:55:44 +02:00
|
|
|
int temp;
|
|
|
|
if(!ToPrimitive<int>(cx, v, temp))
|
|
|
|
return false;
|
|
|
|
if(temp < 0)
|
|
|
|
return false;
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
Storage = (size_t)temp;
|
2005-08-09 17:55:44 +02:00
|
|
|
return true;
|
2004-11-11 08:09:32 +01:00
|
|
|
}
|
|
|
|
|
2009-11-07 13:26:40 +01:00
|
|
|
|
|
|
|
template<> jsval ToJSVal<ssize_t>( const ssize_t& Native )
|
|
|
|
{
|
|
|
|
return( INT_TO_JSVAL( (int)Native ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> jsval ToJSVal<ssize_t>( ssize_t& Native )
|
|
|
|
{
|
|
|
|
return( INT_TO_JSVAL( (int)Native ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> bool ToPrimitive<ssize_t>( JSContext* cx, jsval v, ssize_t& Storage )
|
|
|
|
{
|
|
|
|
int temp;
|
|
|
|
if(!ToPrimitive<int>(cx, v, temp))
|
|
|
|
return false;
|
|
|
|
if(temp < 0)
|
|
|
|
return false;
|
|
|
|
Storage = (ssize_t)temp;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-06-29 23:24:46 +02:00
|
|
|
#endif // #if ARCH_AMD64
|
|
|
|
#endif // #if !GCC_VERSION
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
|
2004-10-07 21:23:35 +02:00
|
|
|
// double
|
|
|
|
|
|
|
|
template<> jsval ToJSVal<double>( const double& Native )
|
|
|
|
{
|
|
|
|
return( DOUBLE_TO_JSVAL( JS_NewDouble( g_ScriptingHost.getContext(), Native ) ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> jsval ToJSVal<double>( double& Native )
|
|
|
|
{
|
|
|
|
return( DOUBLE_TO_JSVAL( JS_NewDouble( g_ScriptingHost.getContext(), Native ) ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> bool ToPrimitive<double>( JSContext* cx, jsval v, double& Storage )
|
|
|
|
{
|
2005-08-09 17:55:44 +02:00
|
|
|
JSBool ok = JS_ValueToNumber(cx, v, &Storage);
|
2006-09-02 21:41:26 +02:00
|
|
|
if (ok == JS_FALSE || !isfinite( Storage ) )
|
2005-08-09 17:55:44 +02:00
|
|
|
return false;
|
|
|
|
return true;
|
2004-10-07 21:23:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// float
|
|
|
|
|
|
|
|
template<> jsval ToJSVal<float>( const float& Native )
|
|
|
|
{
|
|
|
|
return( DOUBLE_TO_JSVAL( JS_NewDouble( g_ScriptingHost.getContext(), Native ) ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> jsval ToJSVal<float>( float& Native )
|
|
|
|
{
|
|
|
|
return( DOUBLE_TO_JSVAL( JS_NewDouble( g_ScriptingHost.getContext(), Native ) ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> bool ToPrimitive<float>( JSContext* cx, jsval v, float& Storage )
|
|
|
|
{
|
2005-08-09 17:55:44 +02:00
|
|
|
double temp;
|
|
|
|
if(!ToPrimitive<double>(cx, v, temp))
|
|
|
|
return false;
|
|
|
|
Storage = (float)temp;
|
|
|
|
return true;
|
2004-10-07 21:23:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// bool
|
|
|
|
|
|
|
|
template<> jsval ToJSVal<bool>( const bool& Native )
|
|
|
|
{
|
|
|
|
return( BOOLEAN_TO_JSVAL( Native ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> jsval ToJSVal<bool>( bool& Native )
|
|
|
|
{
|
|
|
|
return( BOOLEAN_TO_JSVAL( Native ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> bool ToPrimitive<bool>( JSContext* cx, jsval v, bool& Storage )
|
|
|
|
{
|
2005-08-09 17:55:44 +02:00
|
|
|
JSBool temp;
|
|
|
|
JSBool ok = JS_ValueToBoolean(cx, v, &temp);
|
|
|
|
if(ok == JS_FALSE)
|
|
|
|
return false;
|
|
|
|
Storage = (temp == JS_TRUE);
|
|
|
|
return true;
|
2004-10-07 21:23:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CStrW
|
2005-08-09 17:55:44 +02:00
|
|
|
template<> bool ToPrimitive<CStrW>( JSContext* UNUSED(cx), jsval v, CStrW& Storage )
|
2004-10-07 21:23:35 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Storage = g_ScriptingHost.ValueToUCString( v );
|
|
|
|
}
|
|
|
|
catch( PSERROR_Scripting_ConversionFailed )
|
|
|
|
{
|
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> jsval ToJSVal<CStrW>( const CStrW& Native )
|
|
|
|
{
|
2004-11-07 22:30:47 +01:00
|
|
|
return( STRING_TO_JSVAL( JS_NewUCStringCopyZ( g_ScriptingHost.GetContext(), Native.utf16().c_str() ) ) );
|
2004-10-07 21:23:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<> jsval ToJSVal<CStrW>( CStrW& Native )
|
|
|
|
{
|
2004-11-07 22:30:47 +01:00
|
|
|
return( STRING_TO_JSVAL( JS_NewUCStringCopyZ( g_ScriptingHost.GetContext(), Native.utf16().c_str() ) ) );
|
2004-10-07 21:23:35 +02:00
|
|
|
}
|
|
|
|
|
2005-05-03 06:27:07 +02:00
|
|
|
// CStr/CStr8
|
|
|
|
|
2005-08-09 17:55:44 +02:00
|
|
|
template<> bool ToPrimitive<CStr8>( JSContext* UNUSED(cx), jsval v, CStr8& Storage )
|
2005-05-03 06:27:07 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Storage = g_ScriptingHost.ValueToString( v );
|
|
|
|
}
|
|
|
|
catch( PSERROR_Scripting_ConversionFailed )
|
|
|
|
{
|
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> jsval ToJSVal<CStr8>( const CStr8& Native )
|
|
|
|
{
|
|
|
|
return( STRING_TO_JSVAL( JS_NewStringCopyZ( g_ScriptingHost.GetContext(), Native.c_str() ) ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> jsval ToJSVal<CStr8>( CStr8& Native )
|
|
|
|
{
|
|
|
|
return( STRING_TO_JSVAL( JS_NewStringCopyZ( g_ScriptingHost.GetContext(), Native.c_str() ) ) );
|
|
|
|
}
|
|
|
|
|
2004-10-07 21:23:35 +02:00
|
|
|
// jsval
|
|
|
|
|
2007-05-29 21:01:21 +02:00
|
|
|
template<> jsval ToJSVal<jsval_t>( const jsval_t& Native )
|
2004-10-07 21:23:35 +02:00
|
|
|
{
|
2007-05-29 21:01:21 +02:00
|
|
|
return( Native.v );
|
2004-10-07 21:23:35 +02:00
|
|
|
}
|
|
|
|
|
2004-11-27 21:02:51 +01:00
|
|
|
// String->JSVal
|
|
|
|
|
|
|
|
jsval JSParseString( const CStrW& Native )
|
|
|
|
{
|
|
|
|
CParser stringParser;
|
|
|
|
stringParser.InputTaskType( "string", "_$value_" );
|
|
|
|
CParserLine result;
|
2009-11-03 22:46:35 +01:00
|
|
|
result.ParseString( stringParser, CStr(Native) );
|
2004-11-27 21:02:51 +01:00
|
|
|
bool boolResult; int intResult; float floatResult;
|
2004-12-16 10:41:41 +01:00
|
|
|
|
2004-11-27 21:02:51 +01:00
|
|
|
if( result.GetArgFloat( 0, floatResult ) )
|
2004-12-16 10:41:41 +01:00
|
|
|
{
|
|
|
|
if( floatResult == floor( floatResult ) )
|
|
|
|
{
|
|
|
|
intResult = (int)floatResult;
|
|
|
|
if( INT_FITS_IN_JSVAL( intResult ) )
|
|
|
|
return( ToJSVal( intResult ) );
|
|
|
|
}
|
2004-11-27 21:02:51 +01:00
|
|
|
return( ToJSVal( floatResult ) );
|
2004-12-16 10:41:41 +01:00
|
|
|
}
|
|
|
|
if( result.GetArgBool( 0, boolResult ) )
|
|
|
|
return( BOOLEAN_TO_JSVAL( boolResult ) );
|
|
|
|
|
2004-11-27 21:02:51 +01:00
|
|
|
return( ToJSVal( Native ) );
|
2004-12-19 00:30:28 +01:00
|
|
|
}
|