Ticket #2213: Unify_ScriptConversions_v1.2.diff

File Unify_ScriptConversions_v1.2.diff, 32.2 KB (added by Yves, 11 years ago)
  • source/maths/scripting/JSInterface_Vector3D.cpp

     
    1 /* Copyright (C) 2013 Wildfire Games.
    2  * This file is part of 0 A.D.
    3  *
    4  * 0 A.D. is free software: you can redistribute it and/or modify
    5  * it under the terms of the GNU General Public License as published by
    6  * the Free Software Foundation, either version 2 of the License, or
    7  * (at your option) any later version.
    8  *
    9  * 0 A.D. is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
    16  */
    17 
    18 #include "precompiled.h"
    19 
    20 #include "JSInterface_Vector3D.h"
    21 #include "scripting/JSConversions.h"
    22 #include "scripting/ScriptingHost.h"
    23 
    24 JSClass JSI_Vector3D::JSI_class = {
    25     "Vector3D", JSCLASS_HAS_PRIVATE,
    26     JS_PropertyStub, JS_PropertyStub,
    27     JSI_Vector3D::getProperty, JSI_Vector3D::setProperty,
    28     JS_EnumerateStub, JS_ResolveStub,
    29     JS_ConvertStub, JSI_Vector3D::finalize,
    30     NULL, NULL, NULL, NULL
    31 };
    32 
    33 JSPropertySpec JSI_Vector3D::JSI_props[] =
    34 {
    35     { "x", JSI_Vector3D::component_x, JSPROP_ENUMERATE },
    36     { "y", JSI_Vector3D::component_y, JSPROP_ENUMERATE },
    37     { "z", JSI_Vector3D::component_z, JSPROP_ENUMERATE },
    38     { 0 }
    39 };
    40 
    41 JSFunctionSpec JSI_Vector3D::JSI_methods[] =
    42 {
    43     { "toString", JSI_Vector3D::toString, 0, 0 },
    44     { 0 }
    45 };
    46 
    47 void JSI_Vector3D::init()
    48 {
    49     g_ScriptingHost.DefineCustomObjectType(&JSI_class, JSI_Vector3D::construct, 0, JSI_props, JSI_methods, NULL, NULL);
    50 }
    51 
    52 JSI_Vector3D::Vector3D_Info::Vector3D_Info()
    53 {
    54     owner = NULL;
    55     vector = new CVector3D();
    56 }
    57 
    58 JSI_Vector3D::Vector3D_Info::Vector3D_Info(float x, float y, float z)
    59 {
    60     owner = NULL;
    61     vector = new CVector3D(x, y, z);
    62 }
    63 
    64 JSI_Vector3D::Vector3D_Info::Vector3D_Info(const CVector3D& copy)
    65 {
    66     owner = NULL;
    67     vector = new CVector3D(copy.X, copy.Y, copy.Z);
    68 }
    69 
    70 JSI_Vector3D::Vector3D_Info::Vector3D_Info(CVector3D* attach, IPropertyOwner* _owner)
    71 {
    72     owner = _owner;
    73     updateFn = NULL;
    74     freshenFn = NULL;
    75     vector = attach;
    76 }
    77 
    78 JSI_Vector3D::Vector3D_Info::Vector3D_Info(CVector3D* attach, IPropertyOwner* _owner, void(IPropertyOwner::*_updateFn)(void))
    79 {
    80     owner = _owner;
    81     updateFn = _updateFn;
    82     freshenFn = NULL;
    83     vector = attach;
    84 }
    85 
    86 JSI_Vector3D::Vector3D_Info::Vector3D_Info(CVector3D* attach, IPropertyOwner* _owner, void(IPropertyOwner::*_updateFn)(void),
    87         void(IPropertyOwner::*_freshenFn)(void))
    88 {
    89     owner = _owner;
    90     updateFn = _updateFn;
    91     freshenFn = _freshenFn;
    92     vector = attach;
    93 }
    94 
    95 JSI_Vector3D::Vector3D_Info::~Vector3D_Info()
    96 {
    97     if (!owner)
    98         delete (vector);
    99 }
    100 
    101 JSBool JSI_Vector3D::getProperty(JSContext* cx, JSObject* obj, jsid id, jsval* vp)
    102 {
    103     if (!JSID_IS_INT(id))
    104         return JS_TRUE;
    105 
    106     Vector3D_Info* vectorInfo = (Vector3D_Info*)JS_GetInstancePrivate(cx, obj, &JSI_Vector3D::JSI_class, NULL);
    107     if (!vectorInfo)
    108         return JS_FALSE;
    109 
    110     CVector3D* vectorData = vectorInfo->vector;
    111 
    112     if (vectorInfo->owner && vectorInfo->freshenFn)
    113         ((vectorInfo->owner)->*(vectorInfo->freshenFn))();
    114 
    115     switch (JSID_TO_INT(id))
    116     {
    117     case component_x:
    118         return JS_NewNumberValue(cx, vectorData->X, vp);
    119     case component_y:
    120         return JS_NewNumberValue(cx, vectorData->Y, vp);
    121     case component_z:
    122         return JS_NewNumberValue(cx, vectorData->Z, vp);
    123     }
    124 
    125     return JS_FALSE;
    126 }
    127 
    128 JSBool JSI_Vector3D::setProperty(JSContext* cx, JSObject* obj, jsid id, JSBool UNUSED(strict), jsval* vp)
    129 {
    130     if (!JSID_IS_INT(id))
    131         return JS_TRUE;
    132 
    133     Vector3D_Info* vectorInfo = (Vector3D_Info*)JS_GetInstancePrivate(cx, obj, &JSI_Vector3D::JSI_class, NULL);
    134     if (!vectorInfo)
    135         return JS_FALSE;
    136 
    137     CVector3D* vectorData = vectorInfo->vector;
    138 
    139     if (vectorInfo->owner && vectorInfo->freshenFn)
    140         ((vectorInfo->owner)->*(vectorInfo->freshenFn))();
    141 
    142     switch (JSID_TO_INT(id))
    143     {
    144     case component_x:
    145         vectorData->X = ToPrimitive<float> (*vp);
    146         break;
    147     case component_y:
    148         vectorData->Y = ToPrimitive<float> (*vp);
    149         break;
    150     case component_z:
    151         vectorData->Z = ToPrimitive<float> (*vp);
    152         break;
    153     }
    154 
    155     if (vectorInfo->owner && vectorInfo->updateFn)
    156         ((vectorInfo->owner)->*(vectorInfo->updateFn))();
    157 
    158     return JS_TRUE;
    159 }
    160 
    161 JSBool JSI_Vector3D::construct(JSContext* cx, uintN argc, jsval* vp)
    162 {
    163     JSObject* vector = JS_NewObject(cx, &JSI_Vector3D::JSI_class, NULL, NULL);
    164 
    165     if (argc == 0)
    166     {
    167         JS_SetPrivate(cx, vector, new Vector3D_Info());
    168         JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(vector));
    169         return JS_TRUE;
    170     }
    171 
    172     JSU_REQUIRE_PARAMS(3);
    173     try
    174     {
    175         float x = ToPrimitive<float> (JS_ARGV(cx, vp)[0]);
    176         float y = ToPrimitive<float> (JS_ARGV(cx, vp)[1]);
    177         float z = ToPrimitive<float> (JS_ARGV(cx, vp)[2]);
    178         JS_SetPrivate(cx, vector, new Vector3D_Info(x, y, z));
    179         JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(vector));
    180         return JS_TRUE;
    181     }
    182     catch (PSERROR_Scripting_ConversionFailed&)
    183     {
    184         // Invalid input (i.e. can't be coerced into doubles) - fail
    185         JS_ReportError(cx, "Invalid parameters to Vector3D constructor");
    186         return JS_FALSE;
    187     }
    188 }
    189 
    190 void JSI_Vector3D::finalize(JSContext* cx, JSObject* obj)
    191 {
    192     delete ((Vector3D_Info*)JS_GetPrivate(cx, obj));
    193 }
    194 
    195 JSBool JSI_Vector3D::toString(JSContext* cx, uintN UNUSED(argc), jsval* vp)
    196 {
    197     char buffer[256];
    198     Vector3D_Info* vectorInfo = (Vector3D_Info*)JS_GetInstancePrivate(cx, JS_THIS_OBJECT(cx, vp), &JSI_Vector3D::JSI_class, NULL);
    199     if (!vectorInfo)
    200         return JS_FALSE;
    201 
    202     if (vectorInfo->owner && vectorInfo->freshenFn)
    203         ((vectorInfo->owner)->*(vectorInfo->freshenFn))();
    204 
    205     CVector3D* vectorData = vectorInfo->vector;
    206     sprintf_s(buffer, ARRAY_SIZE(buffer), "[object Vector3D: ( %f, %f, %f )]", vectorData->X, vectorData->Y, vectorData->Z);
    207     JS_SET_RVAL(cx, vp, STRING_TO_JSVAL(JS_NewStringCopyZ(cx, buffer)));
    208     return JS_TRUE;
    209 }
  • source/maths/scripting/JSInterface_Vector3D.h

     
    1 /* Copyright (C) 2013 Wildfire Games.
    2  * This file is part of 0 A.D.
    3  *
    4  * 0 A.D. is free software: you can redistribute it and/or modify
    5  * it under the terms of the GNU General Public License as published by
    6  * the Free Software Foundation, either version 2 of the License, or
    7  * (at your option) any later version.
    8  *
    9  * 0 A.D. is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
    16  */
    17 
    18 // JSInterface_Entity.h
    19 //
    20 //
    21 // A JavaScript class representing a Pyrogenesis CVector3D object.
    22 //
    23 // Usage: Used when manipulating objects of class 'Vector3D' in JavaScript.
    24 
    25 #include "scripting/ScriptingHost.h"
    26 #include "maths/Vector3D.h"
    27 
    28 #ifndef INCLUDED_JSI_VECTOR3
    29 #define INCLUDED_JSI_VECTOR3
    30 
    31 namespace JSI_Vector3D
    32 {
    33     enum
    34     {
    35         component_x,
    36         component_y,
    37         component_z
    38     };
    39     JSBool toString(JSContext* cx, uintN argc, jsval* vp);
    40 
    41     struct Vector3D_Info
    42     {
    43         NONCOPYABLE(Vector3D_Info);
    44     public:
    45 
    46         IPropertyOwner* owner;
    47         void (IPropertyOwner::*updateFn)();
    48         void (IPropertyOwner::*freshenFn)();
    49         CVector3D* vector;
    50         Vector3D_Info();
    51         Vector3D_Info(float x, float y, float z);
    52         Vector3D_Info(const CVector3D& copy);
    53         Vector3D_Info(CVector3D* attach, IPropertyOwner* _owner);
    54         Vector3D_Info(CVector3D* attach, IPropertyOwner* _owner, void(IPropertyOwner::*_updateFn)(void));
    55         Vector3D_Info(CVector3D* attach, IPropertyOwner* _owner, void(IPropertyOwner::*_updateFn)(void), void(IPropertyOwner::*_freshenFn)(void));
    56         ~Vector3D_Info();
    57     };
    58     extern JSClass JSI_class;
    59     extern JSPropertySpec JSI_props[];
    60     extern JSFunctionSpec JSI_methods[];
    61 
    62     JSBool getProperty(JSContext* cx, JSObject* obj, jsid id, jsval* vp);
    63     JSBool setProperty(JSContext* cx, JSObject* obj, jsid id, JSBool strict, jsval* vp);
    64     void finalize(JSContext* cx, JSObject* obj);
    65     JSBool construct(JSContext* cx, uintN argc, jsval* vp);
    66     void init();
    67 }
    68 
    69 #endif
  • source/ps/GameSetup/GameSetup.cpp

     
    4848#include "gui/scripting/JSInterface_GUITypes.h"
    4949#include "gui/scripting/ScriptFunctions.h"
    5050#include "maths/MathUtil.h"
    51 #include "maths/scripting/JSInterface_Vector3D.h"
    5251#include "network/NetServer.h"
    5352#include "network/NetClient.h"
    5453
     
    318317
    319318static void RegisterJavascriptInterfaces()
    320319{
    321     // maths
    322     JSI_Vector3D::init();
    323 
    324320    // GUI
    325321    CGUI::ScriptingInit();
    326322
  • source/ps/scripting/JSInterface_VFS.cpp

     
    2323#include "ps/Filesystem.h"
    2424//#include "lib/res/file/archive/vfs_optimizer.h"   // ArchiveBuilderCancel
    2525#include "scripting/ScriptingHost.h"
    26 #include "scripting/JSConversions.h"
     26#include "scriptinterface/ScriptInterface.h"
    2727#include "ps/scripting/JSInterface_VFS.h"
    2828#include "lib/file/vfs/vfs_util.h"
    2929
     
    7070{
    7171    BuildDirEntListState* s = (BuildDirEntListState*)cbData;
    7272
    73     jsval val = ToJSVal( CStrW(pathname.string()) );
     73    jsval val = ScriptInterface::ToJSVal(s->cx, CStrW(pathname.string()));
    7474    JS_SetElement(s->cx, s->filename_array, s->cur_idx++, &val);
    7575    return INFO::OK;
    7676}
     
    9595    JSU_REQUIRE_MIN_PARAMS(1);
    9696
    9797    CStrW path;
    98     if (!ToPrimitive<CStrW> (cx, JS_ARGV(cx, vp)[0], path))
     98    if (!ScriptInterface::FromJSVal(cx, JS_ARGV(cx, vp)[0], path))
    9999        return JS_FALSE;
    100100
    101101    CStrW filter_str = L"";
    102102    if (argc >= 2)
    103103    {
    104         if (!ToPrimitive<CStrW> (cx, JS_ARGV(cx, vp)[1], filter_str))
     104        if (!ScriptInterface::FromJSVal(cx, JS_ARGV(cx, vp)[1], filter_str))
    105105            return JS_FALSE;
    106106    }
    107107    // convert to const wchar_t*; if there's no filter, pass 0 for speed
     
    113113    bool recursive = false;
    114114    if (argc >= 3)
    115115    {
    116         if (!ToPrimitive<bool> (cx, JS_ARGV(cx, vp)[2], recursive))
     116        if (!ScriptInterface::FromJSVal(cx, JS_ARGV(cx, vp)[2], recursive))
    117117            return JS_FALSE;
    118118    }
    119119    int flags = recursive ? vfs::DIR_RECURSIVE : 0;
     
    137137    JSU_REQUIRE_MIN_PARAMS(1);
    138138
    139139    CStrW filename;
    140     if (!ToPrimitive<CStrW> (cx, JS_ARGV(cx, vp)[0], filename))
     140    if (!ScriptInterface::FromJSVal(cx, JS_ARGV(cx, vp)[0], filename))
    141141        return JS_FALSE;
    142142
    143143    CFileInfo fileInfo;
     
    144144    Status err = g_VFS->GetFileInfo(filename, &fileInfo);
    145145    JS_CHECK_FILE_ERR(err);
    146146
    147     JS_SET_RVAL(cx, vp, ToJSVal((double)fileInfo.MTime()));
     147    JS_SET_RVAL(cx, vp, ScriptInterface::ToJSVal(cx, (double)fileInfo.MTime()));
    148148    return JS_TRUE;
    149149}
    150150
     
    158158    JSU_REQUIRE_MIN_PARAMS(1);
    159159
    160160    CStrW filename;
    161     if (!ToPrimitive<CStrW> (cx, JS_ARGV(cx, vp)[0], filename))
     161    if (!ScriptInterface::FromJSVal(cx, JS_ARGV(cx, vp)[0], filename))
    162162        return JS_FALSE;
    163163
    164164    CFileInfo fileInfo;
     
    165165    Status err = g_VFS->GetFileInfo(filename, &fileInfo);
    166166    JS_CHECK_FILE_ERR(err);
    167167
    168     JS_SET_RVAL(cx, vp, ToJSVal( (unsigned)fileInfo.Size() ));
     168    JS_SET_RVAL(cx, vp, ScriptInterface::ToJSVal(cx, (unsigned)fileInfo.Size()));
    169169    return JS_TRUE;
    170170}
    171171
     
    179179    JSU_REQUIRE_MIN_PARAMS(1);
    180180
    181181    CStrW filename;
    182     if (!ToPrimitive<CStrW> (cx, JS_ARGV(cx, vp)[0], filename))
     182    if (!ScriptInterface::FromJSVal(cx, JS_ARGV(cx, vp)[0], filename))
    183183        return JS_FALSE;
    184184
    185185    //
     
    198198    contents.Replace("\r\n", "\n");
    199199
    200200    // Decode as UTF-8
    201     JS_SET_RVAL(cx, vp, ToJSVal( contents.FromUTF8() ));
     201    JS_SET_RVAL(cx, vp, ScriptInterface::ToJSVal(cx, contents.FromUTF8()));
    202202    return JS_TRUE;
    203203}
    204204
     
    212212    JSU_REQUIRE_MIN_PARAMS(1);
    213213
    214214    CStrW filename;
    215     if (!ToPrimitive<CStrW> (cx, JS_ARGV(cx, vp)[0], filename))
     215    if (!ScriptInterface::FromJSVal(cx, JS_ARGV(cx, vp)[0], filename))
    216216        return (JS_FALSE);
    217217
    218218    //
     
    243243    while (std::getline(ss, line))
    244244    {
    245245        // Decode each line as UTF-8
    246         jsval val = ToJSVal(CStr(line).FromUTF8());
     246        jsval val = ScriptInterface::ToJSVal(cx, CStr(line).FromUTF8());
    247247        JS_SetElement(cx, line_array, cur_line++, &val);
    248248    }
    249249
  • source/scripting/JSConversions.cpp

     
    1 /* Copyright (C) 2013 Wildfire Games.
    2  * This file is part of 0 A.D.
    3  *
    4  * 0 A.D. is free software: you can redistribute it and/or modify
    5  * it under the terms of the GNU General Public License as published by
    6  * the Free Software Foundation, either version 2 of the License, or
    7  * (at your option) any later version.
    8  *
    9  * 0 A.D. is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
    16  */
    17 
    18 #include "precompiled.h"
    19 
    20 #include "JSConversions.h"
    21 #include "graphics/ObjectManager.h"
    22 #include "maths/scripting/JSInterface_Vector3D.h"
    23 #include "lib/sysdep/sysdep.h"  // isfinite
    24 #include "scriptinterface/ScriptInterface.h"
    25 #include <math.h>
    26 #include <cfloat>
    27 
    28 // CVector3D
    29 
    30 template<> CVector3D* ToNative<CVector3D>( JSContext* cx, JSObject* obj )
    31 {
    32     JSI_Vector3D::Vector3D_Info* v = (JSI_Vector3D::Vector3D_Info*)JS_GetInstancePrivate( cx, obj, &JSI_Vector3D::JSI_class, NULL );
    33     return( v ? v->vector : NULL );
    34 }
    35 
    36 template<> JSObject* ToScript<CVector3D>( CVector3D* Native )
    37 {
    38     JSObject* Script = JS_NewObject( g_ScriptingHost.GetContext(), &JSI_Vector3D::JSI_class, NULL, NULL );
    39     JS_SetPrivate( g_ScriptingHost.GetContext(), Script, new JSI_Vector3D::Vector3D_Info( *Native ) );
    40     return( Script );
    41 }
    42 
    43 template<> jsval ToJSVal<CVector3D>( const CVector3D& Native )
    44 {
    45     JSObject* Script = JS_NewObject( g_ScriptingHost.GetContext(), &JSI_Vector3D::JSI_class, NULL, NULL );
    46     JS_SetPrivate( g_ScriptingHost.GetContext(), Script, new JSI_Vector3D::Vector3D_Info( Native ) );
    47     return( OBJECT_TO_JSVAL( Script ) );
    48 }
    49 
    50 // int
    51 
    52 template<> jsval ToJSVal<int>( const int& Native )
    53 {
    54     return( INT_TO_JSVAL( Native ) );
    55 }
    56 
    57 template<> jsval ToJSVal<int>( int& Native )
    58 {
    59     return( INT_TO_JSVAL( Native ) );
    60 }
    61 
    62 template<> bool ToPrimitive<int>( JSContext* cx, jsval v, int& Storage )
    63 {
    64     JSBool ok = JS_ValueToInt32(cx, v, (int32*)&Storage);
    65     return ok == JS_TRUE;
    66 }
    67 
    68 // unsigned
    69 
    70 template<> jsval ToJSVal<unsigned>( const unsigned& Native )
    71 {
    72     return( INT_TO_JSVAL( Native ) );
    73 }
    74 
    75 template<> jsval ToJSVal<unsigned>( unsigned& Native )
    76 {
    77     return( INT_TO_JSVAL( Native ) );
    78 }
    79 
    80 template<> bool ToPrimitive<unsigned>( JSContext* cx, jsval v, unsigned& Storage )
    81 {
    82     int temp;
    83     if(!ToPrimitive<int>(cx, v, temp))
    84         return false;
    85     if(temp < 0)
    86         return false;
    87     Storage = (unsigned)temp;
    88     return true;
    89 }
    90 
    91 // long
    92 template<> jsval ToJSVal<long>( const long& Native )
    93 {
    94     return( INT_TO_JSVAL( (int)Native ) );
    95 }
    96 
    97 template<> jsval ToJSVal<long>( long& Native )
    98 {
    99     return( INT_TO_JSVAL( (int)Native ) );
    100 }
    101 
    102 template<> bool ToPrimitive<long>( JSContext* cx, jsval v, long& Storage )
    103 {
    104     int32 tmp;
    105     JSBool ok = JS_ValueToInt32(cx, v, &tmp);
    106     Storage = (long)tmp;
    107     return ok == JS_TRUE;
    108 }
    109 
    110 // unsigned long
    111 template<> jsval ToJSVal<unsigned long>( const unsigned long& Native )
    112 {
    113     return( INT_TO_JSVAL( (int)Native ) );
    114 }
    115 
    116 template<> jsval ToJSVal<unsigned long>( unsigned long& Native )
    117 {
    118     return( INT_TO_JSVAL( (int)Native ) );
    119 }
    120 
    121 template<> bool ToPrimitive<unsigned long>( JSContext* cx, jsval v, unsigned long& Storage )
    122 {
    123     int32 tmp;
    124     JSBool ok = JS_ValueToInt32(cx, v, &tmp);
    125     Storage = (unsigned long)tmp;
    126     return ok == JS_TRUE;
    127 }
    128 
    129 // see comment at declaration of specialization
    130 #if !GCC_VERSION
    131 #if ARCH_AMD64
    132 
    133 template<> jsval ToJSVal<size_t>( const size_t& Native )
    134 {
    135     return( INT_TO_JSVAL( (int)Native ) );
    136 }
    137 
    138 template<> jsval ToJSVal<size_t>( size_t& Native )
    139 {
    140     return( INT_TO_JSVAL( (int)Native ) );
    141 }
    142 
    143 template<> bool ToPrimitive<size_t>( JSContext* cx, jsval v, size_t& Storage )
    144 {
    145     int temp;
    146     if(!ToPrimitive<int>(cx, v, temp))
    147         return false;
    148     if(temp < 0)
    149         return false;
    150     Storage = (size_t)temp;
    151     return true;
    152 }
    153 
    154 
    155 template<> jsval ToJSVal<ssize_t>( const ssize_t& Native )
    156 {
    157     return( INT_TO_JSVAL( (int)Native ) );
    158 }
    159 
    160 template<> jsval ToJSVal<ssize_t>( ssize_t& Native )
    161 {
    162     return( INT_TO_JSVAL( (int)Native ) );
    163 }
    164 
    165 template<> bool ToPrimitive<ssize_t>( JSContext* cx, jsval v, ssize_t& Storage )
    166 {
    167     int temp;
    168     if(!ToPrimitive<int>(cx, v, temp))
    169         return false;
    170     if(temp < 0)
    171         return false;
    172     Storage = (ssize_t)temp;
    173     return true;
    174 }
    175 
    176 #endif  // #if ARCH_AMD64
    177 #endif  // #if !GCC_VERSION
    178 
    179 // double
    180 
    181 template<> jsval ToJSVal<double>( const double& Native )
    182 {
    183     jsval val = JSVAL_VOID;
    184     JS_NewNumberValue( g_ScriptingHost.getContext(), Native, &val );
    185     return val;
    186 }
    187 
    188 template<> jsval ToJSVal<double>( double& Native )
    189 {
    190     jsval val = JSVAL_VOID;
    191     JS_NewNumberValue( g_ScriptingHost.getContext(), Native, &val );
    192     return val;
    193 }
    194 
    195 template<> bool ToPrimitive<double>( JSContext* cx, jsval v, double& Storage )
    196 {
    197     JSBool ok = JS_ValueToNumber(cx, v, &Storage);
    198     if (ok == JS_FALSE || !isfinite( Storage ) )
    199         return false;
    200     return true;
    201 }
    202 
    203 // float
    204 
    205 template<> jsval ToJSVal<float>( const float& Native )
    206 {
    207     jsval val = JSVAL_VOID;
    208     JS_NewNumberValue( g_ScriptingHost.getContext(), Native, &val );
    209     return val;
    210 }
    211 
    212 template<> jsval ToJSVal<float>( float& Native )
    213 {
    214     jsval val = JSVAL_VOID;
    215     JS_NewNumberValue( g_ScriptingHost.getContext(), Native, &val );
    216     return val;
    217 }
    218 
    219 template<> bool ToPrimitive<float>( JSContext* cx, jsval v, float& Storage )
    220 {
    221     double temp;
    222     if(!ToPrimitive<double>(cx, v, temp))
    223         return false;
    224     Storage = (float)temp;
    225     return true;
    226 }
    227    
    228 
    229 // bool
    230 
    231 template<> jsval ToJSVal<bool>( const bool& Native )
    232 {
    233     return( BOOLEAN_TO_JSVAL( Native ) );
    234 }
    235 
    236 template<> jsval ToJSVal<bool>( bool& Native )
    237 {
    238     return( BOOLEAN_TO_JSVAL( Native ) );
    239 }
    240 
    241 template<> bool ToPrimitive<bool>( JSContext* cx, jsval v, bool& Storage )
    242 {
    243     JSBool temp;
    244     JSBool ok = JS_ValueToBoolean(cx, v, &temp);
    245     if(ok == JS_FALSE)
    246         return false;
    247     Storage = (temp == JS_TRUE);
    248     return true;
    249 }
    250 
    251 // CStrW
    252 template<> bool ToPrimitive<CStrW>( JSContext* UNUSED(cx), jsval v, CStrW& Storage )
    253 {
    254     try
    255     {
    256         Storage = g_ScriptingHost.ValueToUCString( v );
    257     }
    258     catch( PSERROR_Scripting_ConversionFailed& )
    259     {
    260         return( false );
    261     }
    262     return( true );
    263 }
    264 
    265 template<> jsval ToJSVal<CStrW>( const CStrW& Native )
    266 {
    267     return( STRING_TO_JSVAL( JS_NewUCStringCopyZ( g_ScriptingHost.GetContext(), reinterpret_cast<const jschar*>(Native.utf16().c_str()) ) ) );
    268 }
    269 
    270 template<> jsval ToJSVal<CStrW>( CStrW& Native )
    271 {
    272     return( STRING_TO_JSVAL( JS_NewUCStringCopyZ( g_ScriptingHost.GetContext(), reinterpret_cast<const jschar*>(Native.utf16().c_str()) ) ) );
    273 }
    274 
    275 // CStr/CStr8
    276 
    277 template<> bool ToPrimitive<CStr8>( JSContext* cx, jsval v, CStr8& Storage )
    278 {
    279     std::string str;
    280     if (!ScriptInterface::FromJSVal(cx, v, str))
    281         return false;
    282     Storage = str;
    283     return true;
    284 }
    285 
    286 template<> jsval ToJSVal<CStr8>( const CStr8& Native )
    287 {
    288     return( STRING_TO_JSVAL( JS_NewStringCopyZ( g_ScriptingHost.GetContext(), Native.c_str() ) ) );
    289 }
    290 
    291 template<> jsval ToJSVal<CStr8>( CStr8& Native )
    292 {
    293     return( STRING_TO_JSVAL( JS_NewStringCopyZ( g_ScriptingHost.GetContext(), Native.c_str() ) ) );
    294 }
  • source/scripting/JSConversions.h

     
    1 /* Copyright (C) 2009 Wildfire Games.
    2  * This file is part of 0 A.D.
    3  *
    4  * 0 A.D. is free software: you can redistribute it and/or modify
    5  * it under the terms of the GNU General Public License as published by
    6  * the Free Software Foundation, either version 2 of the License, or
    7  * (at your option) any later version.
    8  *
    9  * 0 A.D. is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
    16  */
    17 
    18 // A general system of converting between native objects and their JavaScript representations
    19 
    20 #ifndef INCLUDED_JSCONVERSIONS
    21 #define INCLUDED_JSCONVERSIONS
    22 
    23 #include "scripting/ScriptingHost.h"
    24 
    25 class CStrW;
    26 class CScriptObject;
    27 class CObjectEntry;
    28 class CVector3D;
    29 
    30 // -----
    31 //
    32 // Defaults
    33 //
    34 // -----
    35 
    36 template<typename T> T* ToNative( JSContext* cx, JSObject* obj )
    37 {
    38     return( (T*)JS_GetInstancePrivate( cx, obj, &T::JSI_class, NULL ) );
    39 }
    40 
    41 template<typename T> JSObject* ToScript( T* Native )
    42 {
    43     if( !Native )
    44         return( (JSObject*)NULL );
    45     return( Native->GetScript() );
    46 }
    47 
    48 template<typename T> T* ToNative( jsval v )
    49 {
    50     if( !JSVAL_IS_OBJECT( v ) ) return( NULL );
    51     if( v == JSVAL_NULL ) return( NULL );
    52     return( ToNative<T>( g_ScriptingHost.GetContext(), JSVAL_TO_OBJECT( v ) ) );
    53 }
    54 
    55 template<typename T> bool ToPrimitive( JSContext* UNUSED(cx), jsval v, T& Storage )
    56 {
    57     T* Native = ToNative<T>( v );
    58     if( !Native ) return( false );
    59     Storage = *Native;
    60     return( true );
    61 }
    62 
    63 // Handle pointer-to-objects sensibly (by automatically dereferencing them one level)
    64 template<typename T> bool ToPrimitive( JSContext* UNUSED(cx), jsval v, T*& Storage )
    65 {
    66     T* Native = ToNative<T>( v );
    67     if( !Native ) return( false );
    68     Storage = Native;
    69     return( true );
    70 }
    71 
    72 // Throws PSERROR_Scripting_ConversionFailed on failure.
    73 template<typename T> inline T ToPrimitive( JSContext* cx, jsval v )
    74 {
    75     T Temp;
    76     bool ok = ToPrimitive( cx, v, Temp );
    77     if( !ok ) throw PSERROR_Scripting_ConversionFailed();
    78     return( Temp );
    79 }
    80 
    81 // Throws PSERROR_Scripting_ConversionFailed on failure.
    82 template<typename T> inline T ToPrimitive( jsval v )
    83 {
    84     return( ToPrimitive<T>( g_ScriptingHost.GetContext(), v ) );
    85 }
    86 
    87 template<typename T> jsval ToJSVal( T& Native )
    88 {
    89     return( OBJECT_TO_JSVAL( ToScript<T>( &Native ) ) );
    90 }
    91 
    92 template<typename T> jsval ToJSVal( T*& Native )
    93 {
    94     return( OBJECT_TO_JSVAL( ToScript<T>( Native ) ) );
    95 }
    96 
    97 template<typename T> jsval ToJSVal( const T& Native );
    98 
    99 // -----
    100 //
    101 // Overrides
    102 //
    103 // -----
    104 
    105 // CVector3D
    106 template<> CVector3D* ToNative<CVector3D>( JSContext* cx, JSObject* obj );
    107 template<> JSObject* ToScript<CVector3D>( CVector3D* Native );
    108 template<> jsval ToJSVal<CVector3D>( const CVector3D& Native );
    109 
    110 // CObjectEntry
    111 template<> bool ToPrimitive<CObjectEntry>( JSContext* cx, jsval v, CObjectEntry*& Storage );
    112 template<> jsval ToJSVal<CObjectEntry>( CObjectEntry*& Native );
    113 
    114 // CScriptObject
    115 template<> bool ToPrimitive<CScriptObject>( JSContext* cx, jsval v, CScriptObject& Storage );
    116 template<> jsval ToJSVal<CScriptObject>( CScriptObject& Native );
    117 
    118 // int
    119 template<> bool ToPrimitive<int>( JSContext* cx, jsval v, int& Storage );
    120 template<> jsval ToJSVal<int>( const int& Native );
    121 template<> jsval ToJSVal<int>( int& Native );
    122 
    123 // unsigned
    124 template<> bool ToPrimitive<unsigned>( JSContext* cx, jsval v, unsigned& Storage );
    125 template<> jsval ToJSVal<unsigned>( const unsigned& Native );
    126 template<> jsval ToJSVal<unsigned>( unsigned& Native );
    127 
    128 // long int
    129 template<> bool ToPrimitive<long>( JSContext* cx, jsval v, long& Storage );
    130 template<> jsval ToJSVal<long>( const long& Native );
    131 template<> jsval ToJSVal<long>( long& Native );
    132 
    133 // unsigned long int
    134 template<> bool ToPrimitive<unsigned long>( JSContext* cx, jsval v, unsigned long& Storage );
    135 template<> jsval ToJSVal<unsigned long>( const unsigned long& Native );
    136 template<> jsval ToJSVal<unsigned long>( unsigned long& Native );
    137 
    138 // (s)size_t are considered to be identical to (unsigned) int by GCC and
    139 // their specializations would cause conflicts there. On x86_64 GCC, s/size_t
    140 // is equivalent to (unsigned) long, but the same solution applies; use the
    141 // long and unsigned long specializations instead of s/size_t.
    142 #if !GCC_VERSION
    143 
    144 // for some reason, x64 MSC treats size_t as distinct from unsigned long:
    145 #if ARCH_AMD64
    146 
    147 // size_t
    148 template<> bool ToPrimitive<size_t>( JSContext* cx, jsval v, size_t& Storage );
    149 template<> jsval ToJSVal<size_t>( const size_t& Native );
    150 template<> jsval ToJSVal<size_t>( size_t& Native );
    151 
    152 // ssize_t
    153 template<> bool ToPrimitive<ssize_t>( JSContext* cx, jsval v, ssize_t& Storage );
    154 template<> jsval ToJSVal<ssize_t>( const ssize_t& Native );
    155 template<> jsval ToJSVal<ssize_t>( ssize_t& Native );
    156 
    157 #endif
    158 
    159 #endif
    160 
    161 // double
    162 template<> bool ToPrimitive<double>( JSContext* cx, jsval v, double& Storage );
    163 template<> jsval ToJSVal<double>( const double& Native );
    164 template<> jsval ToJSVal<double>( double& Native );
    165 
    166 // float
    167 template<> bool ToPrimitive<float>( JSContext* cx, jsval v, float& Storage );
    168 template<> jsval ToJSVal<float>( const float& Native );
    169 template<> jsval ToJSVal<float>( float& Native );
    170 
    171 // bool
    172 template<> bool ToPrimitive<bool>( JSContext* cx, jsval v, bool& Storage );
    173 template<> jsval ToJSVal<bool>( const bool& Native );
    174 template<> jsval ToJSVal<bool>( bool& Native );
    175 
    176 /*
    177 // char*
    178 template<> bool ToPrimitive<char*>( JSContext* cx, jsval v, char*& Storage );
    179 template<> jsval ToJSVal<char*>( const char* Native );
    180 template<> jsval ToJSVal<char*>( char* Native );
    181 */
    182 
    183 // CStrW
    184 template<> bool ToPrimitive<CStrW>( JSContext* cx, jsval v, CStrW& Storage );
    185 template<> jsval ToJSVal<CStrW>( const CStrW& Native );
    186 template<> jsval ToJSVal<CStrW>( CStrW& Native );
    187 
    188 // CStr(8)
    189 template<> bool ToPrimitive<CStr8>( JSContext* cx, jsval v, CStr8& Storage );
    190 template<> jsval ToJSVal<CStr8>( const CStr8& Native );
    191 template<> jsval ToJSVal<CStr8>( CStr8& Native );
    192 
    193 #endif
  • source/scripting/ScriptGlue.cpp

     
    2424#include "precompiled.h"
    2525
    2626#include "ScriptGlue.h"
    27 #include "JSConversions.h"
    2827
    2928#include "graphics/GameView.h"
    3029#include "graphics/LightEnv.h"
     
    3736#include "lib/svn_revision.h"
    3837#include "lib/timer.h"
    3938#include "lib/sysdep/sysdep.h"  // sys_OpenFile
    40 #include "maths/scripting/JSInterface_Vector3D.h"
    4139#include "network/NetServer.h"
    4240#include "ps/CConsole.h"
    4341#include "ps/CLogger.h"
     
    111109    ONCE(InitJsTimers());
    112110
    113111    JSU_REQUIRE_PARAMS(1);
    114     size_t slot = ToPrimitive<size_t>(JS_ARGV(cx, vp)[0]);
     112    size_t slot;
     113    ScriptInterface::FromJSVal(cx, JS_ARGV(cx, vp)[0], slot);
    115114    if (slot >= MAX_JS_TIMERS)
    116115        return JS_FALSE;
    117116
     
    125124JSBool StopJsTimer(JSContext* cx, uintN argc, jsval* vp)
    126125{
    127126    JSU_REQUIRE_PARAMS(1);
    128     size_t slot = ToPrimitive<size_t>(JS_ARGV(cx, vp)[0]);
     127    size_t slot;
     128    ScriptInterface::FromJSVal(cx, JS_ARGV(cx, vp)[0], slot);
    129129    if (slot >= MAX_JS_TIMERS)
    130130        return JS_FALSE;
    131131
     
    215215
    216216    try
    217217    {
    218         CStr name = ToPrimitive<CStr>(cx, JS_ARGV(cx, vp)[0]);
     218        CStr name;
     219        ScriptInterface::FromJSVal(cx, JS_ARGV(cx, vp)[0], name);
    219220        IGUIObject* guiObj = g_GUI->FindObjectByName(name);
    220221        if (guiObj)
    221222            JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(guiObj->GetJSObject()));
     
    340341
    341342    try
    342343    {
    343         g_Game->m_Paused = ToPrimitive<bool> (JS_ARGV(cx, vp)[0]);
     344        ScriptInterface::FromJSVal(cx, JS_ARGV(cx, vp)[0], g_Game->m_Paused);
    344345
    345346        if ( g_SoundManager )
    346347            g_SoundManager->Pause(g_Game->m_Paused);
  • source/scriptinterface/DebuggingServer.cpp

     
    2121#include "ThreadDebugger.h"
    2222#include "ps/CLogger.h"
    2323#include "ps/Filesystem.h"
    24 #include "scripting/JSConversions.h"
    2524
    2625CDebuggingServer* g_DebuggingServer = NULL;
    2726
  • source/scriptinterface/ScriptConversions.cpp

     
    1 /* Copyright (C) 2012 Wildfire Games.
     1/* Copyright (C) 2013 Wildfire Games.
    22 * This file is part of 0 A.D.
    33 *
    44 * 0 A.D. is free software: you can redistribute it and/or modify
     
    100100    return true;
    101101}
    102102
     103template<> bool ScriptInterface::FromJSVal<long>(JSContext* cx, jsval v, long& out)
     104{
     105    int32 tmp;
     106    JSBool ok = JS_ValueToInt32(cx, v, &tmp);
     107    out = (long)tmp;
     108    return ok == JS_TRUE;
     109}
     110
     111template<> bool ScriptInterface::FromJSVal<unsigned long>(JSContext* cx, jsval v, unsigned long& out)
     112{
     113    int32 tmp;
     114    JSBool ok = JS_ValueToInt32(cx, v, &tmp);
     115    out = (unsigned long)tmp;
     116    return ok == JS_TRUE;
     117}
     118
     119// see comment at declaration of specialization
     120#if !GCC_VERSION
     121#if ARCH_AMD64
     122
     123template<> bool ScriptInterface::FromJSVal<size_t>(JSContext* cx, jsval v, size_t& out)
     124{
     125    int temp;
     126    if(!FromJSVal<int>(cx, v, temp))
     127        return false;
     128    if(temp < 0)
     129        return false;
     130    out = (size_t)temp;
     131    return true;
     132}
     133
     134template<> bool ScriptInterface::FromJSVal<ssize_t>(JSContext* cx, jsval v, ssize_t& out)
     135{
     136    int temp;
     137    if(!FromJSVal<int>(cx, v, temp))
     138        return false;
     139    if(temp < 0)
     140        return false;
     141    out = (ssize_t)temp;
     142    return true;
     143}
     144
     145#endif  // #if ARCH_AMD64
     146#endif  // #if !GCC_VERSION
     147
    103148// NOTE: we can't define a jsval specialisation, because that conflicts with integer types
    104149template<> bool ScriptInterface::FromJSVal<CScriptVal>(JSContext* UNUSED(cx), jsval v, CScriptVal& out)
    105150{
     
    150195    return true;
    151196}
    152197
     198template<> bool ScriptInterface::FromJSVal<CStr8>(JSContext* cx, jsval v, CStr8& out)
     199{
     200    LOGWARNING(L"FromJSVal Str8");
     201    return ScriptInterface::FromJSVal(cx, v, static_cast<std::string&>(out));
     202}
     203
     204template<> bool ScriptInterface::FromJSVal<CStrW>(JSContext* cx, jsval v, CStrW& out)
     205{
     206    return ScriptInterface::FromJSVal(cx, v, static_cast<std::wstring&>(out));
     207}
     208
    153209template<> bool ScriptInterface::FromJSVal<Entity>(JSContext* cx, jsval v, Entity& out)
    154210{
    155211    JSObject* obj;
     
    220276    return rval;
    221277}
    222278
     279template<> jsval ScriptInterface::ToJSVal<long>(JSContext* UNUSED(cx), const long& val)
     280{
     281    return INT_TO_JSVAL((int)val);
     282}
     283
     284template<> jsval ScriptInterface::ToJSVal<unsigned long>(JSContext* UNUSED(cx), const unsigned long& val)
     285{
     286    return INT_TO_JSVAL((int)val);
     287}
     288
     289// (s)size_t are considered to be identical to (unsigned) int by GCC and
     290// their specializations would cause conflicts there. On x86_64 GCC, s/size_t
     291// is equivalent to (unsigned) long, but the same solution applies; use the
     292// long and unsigned long specializations instead of s/size_t.
     293#if !GCC_VERSION
     294
     295// for some reason, x64 MSC treats size_t as distinct from unsigned long:
     296#if ARCH_AMD64
     297
     298template<> jsval ScriptInterface::ToJSVal<size_t>(JSContext* UNUSED(cx), const size_t& val)
     299{
     300    return INT_TO_JSVAL((int)val);
     301}
     302
     303template<> jsval ScriptInterface::ToJSVal<ssize_t>(JSContext* UNUSED(cx), const ssize_t& val)
     304{
     305    return INT_TO_JSVAL((int)val);
     306}
     307
     308#endif  // #if ARCH_AMD64
     309#endif  // #if !GCC_VERSION
     310
    223311// NOTE: we can't define a jsval specialisation, because that conflicts with integer types
    224312template<> jsval ScriptInterface::ToJSVal<CScriptVal>(JSContext* UNUSED(cx), const CScriptVal& val)
    225313{
     
    281369
    282370template<typename T> static jsval ToJSVal_vector(JSContext* cx, const std::vector<T>& val)
    283371{
    284     JSObject* obj = JS_NewArrayObject(cx, val.size(), NULL);
     372    JSObject* obj = JS_NewArrayObject(cx, (jsint)val.size(), NULL);
    285373    if (!obj)
    286374        return JSVAL_VOID;
    287375    for (size_t i = 0; i < val.size(); ++i)