summaryrefslogtreecommitdiffstats
path: root/o3d/gpu_plugin/np_utils/np_utils.cc
blob: d1400e6b578f27761ab90eef1f800565d096fb06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "o3d/gpu_plugin/np_utils/np_utils.h"

namespace gpu_plugin {

bool NPVariantToValue(bool* value, const NPVariant& variant) {
  if (NPVARIANT_IS_BOOLEAN(variant)) {
    *value = NPVARIANT_TO_BOOLEAN(variant);
    return true;
  }

  return false;
}

bool NPVariantToValue(int32* value, const NPVariant& variant) {
  if (NPVARIANT_IS_INT32(variant)) {
    *value = NPVARIANT_TO_INT32(variant);
    return true;
  }

  return false;
}

bool NPVariantToValue(float* value, const NPVariant& variant) {
  if (NPVARIANT_IS_DOUBLE(variant)) {
    *value = static_cast<float>(NPVARIANT_TO_DOUBLE(variant));
    return true;
  } else if (NPVARIANT_IS_INT32(variant)) {
    *value = static_cast<float>(NPVARIANT_TO_INT32(variant));
    return true;
  }

  return false;
}

bool NPVariantToValue(double* value, const NPVariant& variant) {
  if (NPVARIANT_IS_DOUBLE(variant)) {
    *value = NPVARIANT_TO_DOUBLE(variant);
    return true;
  } else if (NPVARIANT_IS_INT32(variant)) {
    *value = NPVARIANT_TO_INT32(variant);
    return true;
  }

  return false;
}

bool NPVariantToValue(std::string* value, const NPVariant& variant) {
  if (NPVARIANT_IS_STRING(variant)) {
    const NPString& str = NPVARIANT_TO_STRING(variant);
    *value = std::string(str.UTF8Characters, str.UTF8Length);
    return true;
  }

  return false;
}

void ValueToNPVariant(bool value, NPVariant* variant) {
  BOOLEAN_TO_NPVARIANT(value, *variant);
}

void ValueToNPVariant(int32 value, NPVariant* variant) {
  INT32_TO_NPVARIANT(value, *variant);
}

void ValueToNPVariant(float value, NPVariant* variant) {
  DOUBLE_TO_NPVARIANT(value, *variant);
}

void ValueToNPVariant(double value, NPVariant* variant) {
  DOUBLE_TO_NPVARIANT(value, *variant);
}

void ValueToNPVariant(const std::string& value, NPVariant* variant) {
  NPUTF8* p = static_cast<NPUTF8*>(NPBrowser::get()->MemAlloc(value.length()));
  memcpy(p, value.c_str(), value.length());
  STRINGN_TO_NPVARIANT(p, value.length(), *variant);
}

SmartNPVariant::SmartNPVariant() {
  VOID_TO_NPVARIANT(*this);
}

SmartNPVariant::SmartNPVariant(const SmartNPVariant& rhs) {
  rhs.CopyTo(this);
}

SmartNPVariant::SmartNPVariant(const NPVariant& rhs) {
  static_cast<const SmartNPVariant&>(rhs).CopyTo(this);
}

SmartNPVariant::~SmartNPVariant() {
  Release();
}

SmartNPVariant& SmartNPVariant::operator=(const SmartNPVariant& rhs) {
  Release();
  rhs.CopyTo(this);
  return *this;
}

SmartNPVariant& SmartNPVariant::operator=(const NPVariant& rhs) {
  Release();
  static_cast<const SmartNPVariant&>(rhs).CopyTo(this);
  return *this;
}

bool SmartNPVariant::IsVoid() const {
  return NPVARIANT_IS_VOID(*this);
}

void SmartNPVariant::Release() {
  NPBrowser::get()->ReleaseVariantValue(this);
  VOID_TO_NPVARIANT(*this);
}

void SmartNPVariant::Invalidate() {
  if (NPVARIANT_IS_OBJECT(*this)) {
    NULL_TO_NPVARIANT(*this);
  }
}

void SmartNPVariant::CopyTo(NPVariant* rhs) const {
  if (NPVARIANT_IS_OBJECT(*this)) {
    NPObject* object = NPVARIANT_TO_OBJECT(*this);
    OBJECT_TO_NPVARIANT(object, *rhs);
    NPBrowser::get()->RetainObject(object);
  } else if (NPVARIANT_IS_STRING(*this)) {
    NPUTF8* copy = static_cast<NPUTF8*>(NPBrowser::get()->MemAlloc(
        value.stringValue.UTF8Length));
    memcpy(copy,
           value.stringValue.UTF8Characters,
           value.stringValue.UTF8Length);
    STRINGN_TO_NPVARIANT(copy, value.stringValue.UTF8Length, *rhs);
  } else {
    memcpy(rhs, this, sizeof(*rhs));
  }
}

bool NPHasMethod(NPP npp,
                 const NPObjectPointer<NPObject>& object,
                 const NPUTF8* name) {
  return NPBrowser::get()->HasMethod(
      npp,
      object.Get(),
      NPBrowser::get()->GetStringIdentifier(name));
}

bool NPHasProperty(NPP npp,
                   const NPObjectPointer<NPObject>& object,
                   const NPUTF8* name) {
  return NPBrowser::get()->HasProperty(
      npp,
      object.Get(),
      NPBrowser::get()->GetStringIdentifier(name));
}

bool NPRemoveProperty(NPP npp,
                      const NPObjectPointer<NPObject>& object,
                      const NPUTF8* name) {
  return NPBrowser::get()->RemoveProperty(
      npp,
      object.Get(),
      NPBrowser::get()->GetStringIdentifier(name));
}

}  // namespace gpu_plugin