| 1 | /* |
| 2 | * Copyright (C) 2013 Apple Inc. All rights reserved. |
| 3 | * Copyright (C) 2013 Cisco Systems, Inc. All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 15 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 16 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 18 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 19 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 20 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 22 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 23 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 24 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
| 26 | |
| 27 | #include "PluginTest.h" |
| 28 | |
| 29 | #include <string.h> |
| 30 | |
| 31 | using namespace std; |
| 32 | |
| 33 | class ToStringAndValueOfObject : public PluginTest { |
| 34 | public: |
| 35 | ToStringAndValueOfObject(NPP npp, const string& identifier) |
| 36 | : PluginTest(npp, identifier) |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | private: |
| 41 | class ScriptableObject : public Object<ScriptableObject> { |
| 42 | public: |
| 43 | bool hasMethod(NPIdentifier methodName) |
| 44 | { |
| 45 | return identifierIs(methodName, "toString" ) |
| 46 | || identifierIs(methodName, "valueOf" ); |
| 47 | } |
| 48 | |
| 49 | bool invoke(NPIdentifier methodName, const NPVariant*, uint32_t, NPVariant* result) |
| 50 | { |
| 51 | if (identifierIs(methodName, "toString" )) { |
| 52 | // Return classname as string. |
| 53 | char* className = static_cast<char*>(pluginTest()->NPN_MemAlloc(11)); |
| 54 | strcpy(className, "TestObject" ); |
| 55 | STRINGZ_TO_NPVARIANT(className, *result); |
| 56 | return true; |
| 57 | } |
| 58 | if (identifierIs(methodName, "valueOf" )) { |
| 59 | // Return some number. |
| 60 | DOUBLE_TO_NPVARIANT(123456789, *result); |
| 61 | return true; |
| 62 | } |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | private: |
| 67 | ToStringAndValueOfObject* pluginTest() const { return static_cast<ToStringAndValueOfObject*>(Object<ScriptableObject>::pluginTest()); } |
| 68 | }; |
| 69 | |
| 70 | virtual NPError NPP_GetValue(NPPVariable variable, void* value) |
| 71 | { |
| 72 | if (variable != NPPVpluginScriptableNPObject) |
| 73 | return NPERR_GENERIC_ERROR; |
| 74 | |
| 75 | *(NPObject**)value = ScriptableObject::create(this); |
| 76 | |
| 77 | return NPERR_NO_ERROR; |
| 78 | } |
| 79 | }; |
| 80 | |
| 81 | static PluginTest::Register<ToStringAndValueOfObject> ToStringAndValueOfObject("to-string-and-value-of-object" ); |
| 82 | |