jat@google.com | 64a55cb | 2009-10-16 14:16:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008 Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | * use this file except in compliance with the License. You may obtain a copy of |
| 6 | * the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | * License for the specific language governing permissions and limitations under |
| 14 | * the License. |
| 15 | */ |
| 16 | |
| 17 | #include <iostream> |
| 18 | #include <map> |
| 19 | #include <JavaScriptCore/JavaScriptCore.h> |
| 20 | #include "trace.h" |
| 21 | |
| 22 | #ifdef ENABLE_JSVALUE_PROTECTION_CHECKING |
| 23 | static std::map<JSValueRef, int> _protectMap; |
| 24 | #endif |
| 25 | |
| 26 | /* |
| 27 | * |
| 28 | */ |
| 29 | void JSValueProtectChecked(JSContextRef jsContext, JSValueRef jsValue) { |
| 30 | JSValueProtect(jsContext, jsValue); |
| 31 | #ifdef ENABLE_JSVALUE_PROTECTION_CHECKING |
| 32 | _protectMap[jsValue]++; |
| 33 | #endif |
| 34 | } |
| 35 | |
| 36 | /* |
| 37 | * |
| 38 | */ |
| 39 | void JSValueUnprotectChecked(JSContextRef jsContext, JSValueRef jsValue) { |
| 40 | #ifdef ENABLE_JSVALUE_PROTECTION_CHECKING |
| 41 | // Unrecord in a hash_map |
| 42 | unsigned count = _protectMap[jsValue]; |
| 43 | if (count == 0) { |
| 44 | std::cerr << "[WARNING] JSValueUnprotect called on unprotected JSValueRef (0x" |
| 45 | << std::hex << ((unsigned)jsValue) << ")" << std::endl; |
| 46 | return; |
| 47 | } |
| 48 | _protectMap[jsValue] = count - 1; |
| 49 | #else |
| 50 | JSValueUnprotect(jsContext, jsValue); |
| 51 | #endif |
| 52 | } |
| 53 | |
| 54 | bool JSValueIsProtected(JSValueRef jsValue) { |
| 55 | #ifdef ENABLE_JSVALUE_PROTECTION_CHECKING |
| 56 | return _protectMap[jsValue] > 0; |
| 57 | #else |
| 58 | return true; |
| 59 | #endif |
| 60 | } |
| 61 | |
| 62 | bool JSValueProtectCheckingIsEnabled() { |
| 63 | #ifdef ENABLE_JSVALUE_PROTECTION_CHECKING |
| 64 | return true; |
| 65 | #else |
| 66 | return false; |
| 67 | #endif |
| 68 | } |