| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2009, Google Inc. | 2 * Copyright 2009, Google Inc. |
| 3 * All rights reserved. | 3 * All rights reserved. |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions are | 6 * modification, are permitted provided that the following conditions are |
| 7 * met: | 7 * met: |
| 8 * | 8 * |
| 9 * * Redistributions of source code must retain the above copyright | 9 * * Redistributions of source code must retain the above copyright |
| 10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 | 32 |
| 33 // Code relating to interoperation of V8 JavaScript engine with NPAPI. | 33 // Code relating to interoperation of V8 JavaScript engine with NPAPI. |
| 34 | 34 |
| 35 #ifndef O3D_PLUGIN_CROSS_NP_V8_BRIDGE_H_ | 35 #ifndef O3D_PLUGIN_CROSS_NP_V8_BRIDGE_H_ |
| 36 #define O3D_PLUGIN_CROSS_NP_V8_BRIDGE_H_ | 36 #define O3D_PLUGIN_CROSS_NP_V8_BRIDGE_H_ |
| 37 | 37 |
| 38 #include <npapi.h> | 38 #include <npapi.h> |
| 39 #include <npruntime.h> | 39 #include <npruntime.h> |
| 40 #include <map> | 40 #include <map> |
| 41 | 41 |
| 42 #include "base/cross/std_hash.h" | 42 #include "base/hash_tables.h" |
| 43 #include "core/cross/error_status.h" | 43 #include "core/cross/error_status.h" |
| 44 #include "core/cross/service_dependency.h" | 44 #include "core/cross/service_dependency.h" |
| 45 #include "core/cross/types.h" | 45 #include "core/cross/types.h" |
| 46 | 46 |
| 47 // The XLib header files define these preprocessor macros which v8 uses as | 47 // The XLib header files define these preprocessor macros which v8 uses as |
| 48 // identifiers. Need to undefine them before including v8. | 48 // identifiers. Need to undefine them before including v8. |
| 49 #ifdef None | 49 #ifdef None |
| 50 #undef None | 50 #undef None |
| 51 #endif | 51 #endif |
| 52 | 52 |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 void Release() { | 172 void Release() { |
| 173 if (owned && object_ != NULL) { | 173 if (owned && object_ != NULL) { |
| 174 NPN_ReleaseObject(object_); | 174 NPN_ReleaseObject(object_); |
| 175 } | 175 } |
| 176 } | 176 } |
| 177 | 177 |
| 178 mutable bool owned; | 178 mutable bool owned; |
| 179 T* object_; | 179 T* object_; |
| 180 }; | 180 }; |
| 181 | 181 |
| 182 } // namespace o3d |
| 183 |
| 182 // Hashes an NPObject so it can be used in a hash_map. | 184 // Hashes an NPObject so it can be used in a hash_map. |
| 183 template <typename T> | 185 #if defined(COMPILER_GCC) |
| 184 class NPObjectPtrHash { | 186 namespace __gnu_cxx { |
| 185 public: | 187 |
| 186 size_t operator() (const NPObjectPtr<T>& ptr) const { | 188 template<class T> |
| 187 return o3d::base::hash<size_t>()(reinterpret_cast<size_t>(ptr.Get())); | 189 struct hash<o3d::NPObjectPtr<T> > { |
| 190 std::size_t operator()(const o3d::NPObjectPtr<T>& ptr) const { |
| 191 return hash<size_t>()(reinterpret_cast<size_t>(ptr.Get())); |
| 188 } | 192 } |
| 189 }; | 193 }; |
| 190 | 194 |
| 195 } // namespace __gnu_cxx |
| 196 #elif defined(COMPILER_MSVC) |
| 197 namespace stdext { |
| 198 |
| 199 template<class T> |
| 200 inline size_t hash_value(const o3d::NPObjectPtr<T>& ptr) { |
| 201 return hash_value(reinterpret_cast<size_t>(ptr.Get())); |
| 202 } |
| 203 |
| 204 } // namespace stdext |
| 205 #endif // COMPILER |
| 206 |
| 207 namespace o3d { |
| 208 |
| 191 // A V8 handle that automatically disposes itself when it is destroyed. There | 209 // A V8 handle that automatically disposes itself when it is destroyed. There |
| 192 // must be only one of these for each persistent handle, otherwise they might | 210 // must be only one of these for each persistent handle, otherwise they might |
| 193 // be disposed more than once. | 211 // be disposed more than once. |
| 194 template <typename T> | 212 template <typename T> |
| 195 class AutoV8Persistent : public v8::Persistent<T> { | 213 class AutoV8Persistent : public v8::Persistent<T> { |
| 196 public: | 214 public: |
| 197 AutoV8Persistent() {} | 215 AutoV8Persistent() {} |
| 198 | 216 |
| 199 template <typename U> | 217 template <typename U> |
| 200 explicit AutoV8Persistent(const v8::Persistent<U>& rhs) | 218 explicit AutoV8Persistent(const v8::Persistent<U>& rhs) |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 362 const v8::AccessorInfo& info); | 380 const v8::AccessorInfo& info); |
| 363 | 381 |
| 364 static v8::Handle<v8::Value> V8CallNamedMethod(const v8::Arguments& args); | 382 static v8::Handle<v8::Value> V8CallNamedMethod(const v8::Arguments& args); |
| 365 | 383 |
| 366 static v8::Handle<v8::Value> V8CallFunction(const v8::Arguments& args); | 384 static v8::Handle<v8::Value> V8CallFunction(const v8::Arguments& args); |
| 367 | 385 |
| 368 static v8::Handle<v8::Value> V8CallAsFunction(const v8::Arguments& args); | 386 static v8::Handle<v8::Value> V8CallAsFunction(const v8::Arguments& args); |
| 369 | 387 |
| 370 NPObjectPtr<NPObject> GetNPConstructFunction(int arity); | 388 NPObjectPtr<NPObject> GetNPConstructFunction(int arity); |
| 371 | 389 |
| 372 typedef o3d::base::hash_map<NPObjectPtr<NPObject>, | 390 typedef ::base::hash_map<NPObjectPtr<NPObject>, |
| 373 AutoV8Persistent<v8::Object>, | 391 AutoV8Persistent<v8::Object> > NPV8ObjectMap; |
| 374 NPObjectPtrHash<NPObject> > NPV8ObjectMap; | |
| 375 | 392 |
| 376 typedef std::map<int, NPObjectPtr<NPObject> > NPConstructFunctionMap; | 393 typedef std::map<int, NPObjectPtr<NPObject> > NPConstructFunctionMap; |
| 377 | 394 |
| 378 ServiceLocator* service_locator_; | 395 ServiceLocator* service_locator_; |
| 379 ServiceDependency<IErrorStatus> error_status_; | 396 ServiceDependency<IErrorStatus> error_status_; |
| 380 NPP npp_; | 397 NPP npp_; |
| 381 NPObjectPtr<NPObject> global_np_object_; | 398 NPObjectPtr<NPObject> global_np_object_; |
| 382 AutoV8Persistent<v8::Context> script_context_; | 399 AutoV8Persistent<v8::Context> script_context_; |
| 383 AutoV8Persistent<v8::FunctionTemplate> v8_np_constructor_template_; | 400 AutoV8Persistent<v8::FunctionTemplate> v8_np_constructor_template_; |
| 384 AutoV8Persistent<v8::Object> function_map_; | 401 AutoV8Persistent<v8::Object> function_map_; |
| 385 AutoV8Persistent<v8::Object> global_prototype_; | 402 AutoV8Persistent<v8::Object> global_prototype_; |
| 386 NPV8ObjectMap np_v8_object_map_; | 403 NPV8ObjectMap np_v8_object_map_; |
| 387 NPObjectPtr<NPObject> np_enumerate_function_; | 404 NPObjectPtr<NPObject> np_enumerate_function_; |
| 388 NPObjectPtr<NPObject> np_is_function_function_; | 405 NPObjectPtr<NPObject> np_is_function_function_; |
| 389 NPObjectPtr<NPObject> np_wrap_function_function_; | 406 NPObjectPtr<NPObject> np_wrap_function_function_; |
| 390 NPConstructFunctionMap np_construct_functions_; | 407 NPConstructFunctionMap np_construct_functions_; |
| 391 NPIdentifier np_name_identifier_; | 408 NPIdentifier np_name_identifier_; |
| 392 NPIdentifier np_call_identifier_; | 409 NPIdentifier np_call_identifier_; |
| 393 NPIdentifier np_length_identifier_; | 410 NPIdentifier np_length_identifier_; |
| 394 NPIdentifier np_proxy_identifier_; | 411 NPIdentifier np_proxy_identifier_; |
| 395 NPObjectPtr<NPObject> np_empty_array_; | 412 NPObjectPtr<NPObject> np_empty_array_; |
| 396 DISALLOW_COPY_AND_ASSIGN(NPV8Bridge); | 413 DISALLOW_COPY_AND_ASSIGN(NPV8Bridge); |
| 397 }; | 414 }; |
| 398 } // namespace o3d | 415 } // namespace o3d |
| 399 | 416 |
| 400 #endif // O3D_PLUGIN_CROSS_NP_V8_BRIDGE_H_ | 417 #endif // O3D_PLUGIN_CROSS_NP_V8_BRIDGE_H_ |
| OLD | NEW |