14d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann// Copyright 2014 PDFium Authors. All rights reserved.
24d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann// Use of this source code is governed by a BSD-style license that can be
34d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann// found in the LICENSE file.
44d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
54d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
64d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
74d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann#include "fxjs/cfxjse_value.h"
84d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
94d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann#include <math.h>
104d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
114d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann#include "fxjs/cfxjse_class.h"
124d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann#include "fxjs/cfxjse_context.h"
134d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
144d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannnamespace {
154d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
164d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmanndouble ftod(FX_FLOAT fNumber) {
174d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  static_assert(sizeof(FX_FLOAT) == 4, "FX_FLOAT of incorrect size");
184d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
194d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  uint32_t nFloatBits = (uint32_t&)fNumber;
204d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  uint8_t nExponent = (uint8_t)(nFloatBits >> 23);
214d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  if (nExponent == 0 || nExponent == 255)
224d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    return fNumber;
234d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
244d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  int8_t nErrExp = nExponent - 150;
254d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  if (nErrExp >= 0)
264d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    return fNumber;
274d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
284d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  double dwError = pow(2.0, nErrExp), dwErrorHalf = dwError / 2;
294d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  double dNumber = fNumber, dNumberAbs = fabs(fNumber);
304d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  double dNumberAbsMin = dNumberAbs - dwErrorHalf,
314d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann         dNumberAbsMax = dNumberAbs + dwErrorHalf;
324d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  int32_t iErrPos = 0;
334d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  if (floor(dNumberAbsMin) == floor(dNumberAbsMax)) {
344d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    dNumberAbsMin = fmod(dNumberAbsMin, 1.0);
354d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    dNumberAbsMax = fmod(dNumberAbsMax, 1.0);
364d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    int32_t iErrPosMin = 1, iErrPosMax = 38;
374d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    do {
384d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      int32_t iMid = (iErrPosMin + iErrPosMax) / 2;
394d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      double dPow = pow(10.0, iMid);
404d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      if (floor(dNumberAbsMin * dPow) == floor(dNumberAbsMax * dPow)) {
414d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann        iErrPosMin = iMid + 1;
424d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      } else {
434d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann        iErrPosMax = iMid;
444d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      }
454d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    } while (iErrPosMin < iErrPosMax);
464d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    iErrPos = iErrPosMax;
474d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  }
484d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  double dPow = pow(10.0, iErrPos);
494d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  return fNumber < 0 ? ceil(dNumber * dPow - 0.5) / dPow
504d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann                     : floor(dNumber * dPow + 0.5) / dPow;
514d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann}
524d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
534d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann}  // namespace
544d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
554d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannvoid FXJSE_ThrowMessage(const CFX_ByteStringC& utf8Message) {
564d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Isolate* pIsolate = v8::Isolate::GetCurrent();
574d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  ASSERT(pIsolate);
584d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
594d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  CFXJSE_ScopeUtil_IsolateHandleRootContext scope(pIsolate);
604d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::String> hMessage = v8::String::NewFromUtf8(
614d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      pIsolate, utf8Message.c_str(), v8::String::kNormalString,
624d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      utf8Message.GetLength());
634d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> hError = v8::Exception::Error(hMessage);
644d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  pIsolate->ThrowException(hError);
654d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann}
664d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
674d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. MoltmannCFXJSE_Value::CFXJSE_Value(v8::Isolate* pIsolate) : m_pIsolate(pIsolate) {}
684d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
694d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. MoltmannCFXJSE_Value::~CFXJSE_Value() {}
704d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
714d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. MoltmannCFXJSE_HostObject* CFXJSE_Value::ToHostObject(CFXJSE_Class* lpClass) const {
724d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  ASSERT(!m_hValue.IsEmpty());
734d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
744d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
754d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> pValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
764d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  ASSERT(!pValue.IsEmpty());
774d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
784d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  if (!pValue->IsObject())
794d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    return nullptr;
804d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
814d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  return FXJSE_RetrieveObjectBinding(pValue.As<v8::Object>(), lpClass);
824d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann}
834d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
844d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannvoid CFXJSE_Value::SetObject(CFXJSE_HostObject* lpObject,
854d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann                             CFXJSE_Class* pClass) {
864d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  if (!pClass) {
874d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    ASSERT(!lpObject);
884d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    SetJSObject();
894d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    return;
904d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  }
914d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  SetHostObject(lpObject, pClass);
924d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann}
934d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
944d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannvoid CFXJSE_Value::SetHostObject(CFXJSE_HostObject* lpObject,
954d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann                                 CFXJSE_Class* lpClass) {
964d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
974d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  ASSERT(lpClass);
984d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::FunctionTemplate> hClass =
994d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      v8::Local<v8::FunctionTemplate>::New(m_pIsolate, lpClass->m_hTemplate);
1004d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Object> hObject = hClass->InstanceTemplate()->NewInstance();
1014d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  FXJSE_UpdateObjectBinding(hObject, lpObject);
1024d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  m_hValue.Reset(m_pIsolate, hObject);
1034d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann}
1044d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
1054d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannvoid CFXJSE_Value::SetArray(
1064d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    const std::vector<std::unique_ptr<CFXJSE_Value>>& values) {
1074d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
1084d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Array> hArrayObject = v8::Array::New(m_pIsolate, values.size());
1094d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  uint32_t count = 0;
1104d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  for (auto& v : values) {
1114d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    hArrayObject->Set(count++, v8::Local<v8::Value>::New(
1124d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann                                   m_pIsolate, v.get()->DirectGetValue()));
1134d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  }
1144d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  m_hValue.Reset(m_pIsolate, hArrayObject);
1154d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann}
1164d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
1174d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannvoid CFXJSE_Value::SetDate(double dDouble) {
1184d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
1194d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> hDate = v8::Date::New(m_pIsolate, dDouble);
1204d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  m_hValue.Reset(m_pIsolate, hDate);
1214d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann}
1224d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
1234d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannvoid CFXJSE_Value::SetFloat(FX_FLOAT fFloat) {
1244d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
1254d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> pValue = v8::Number::New(m_pIsolate, ftod(fFloat));
1264d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  m_hValue.Reset(m_pIsolate, pValue);
1274d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann}
1284d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
1294d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannbool CFXJSE_Value::SetObjectProperty(const CFX_ByteStringC& szPropName,
1304d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann                                     CFXJSE_Value* lpPropValue) {
1314d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  ASSERT(lpPropValue);
1324d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
1334d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> hObject =
1344d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
1354d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  if (!hObject->IsObject())
1364d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    return false;
1374d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
1384d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> hPropValue =
1394d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      v8::Local<v8::Value>::New(m_pIsolate, lpPropValue->DirectGetValue());
1404d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  return (bool)hObject.As<v8::Object>()->Set(
1414d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      v8::String::NewFromUtf8(m_pIsolate, szPropName.c_str(),
1424d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann                              v8::String::kNormalString,
1434d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann                              szPropName.GetLength()),
1444d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      hPropValue);
1454d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann}
1464d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
1474d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannbool CFXJSE_Value::GetObjectProperty(const CFX_ByteStringC& szPropName,
1484d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann                                     CFXJSE_Value* lpPropValue) {
1494d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  ASSERT(lpPropValue);
1504d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
1514d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> hObject =
1524d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
1534d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  if (!hObject->IsObject())
1544d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    return false;
1554d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
1564d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> hPropValue =
1574d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      hObject.As<v8::Object>()->Get(v8::String::NewFromUtf8(
1584d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann          m_pIsolate, szPropName.c_str(), v8::String::kNormalString,
1594d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann          szPropName.GetLength()));
1604d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  lpPropValue->ForceSetValue(hPropValue);
1614d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  return true;
1624d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann}
1634d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
1644d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannbool CFXJSE_Value::SetObjectProperty(uint32_t uPropIdx,
1654d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann                                     CFXJSE_Value* lpPropValue) {
1664d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
1674d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> hObject =
1684d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
1694d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  if (!hObject->IsObject())
1704d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    return false;
1714d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
1724d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> hPropValue =
1734d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      v8::Local<v8::Value>::New(m_pIsolate, lpPropValue->DirectGetValue());
1744d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  return (bool)hObject.As<v8::Object>()->Set(uPropIdx, hPropValue);
1754d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann}
1764d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
1774d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannbool CFXJSE_Value::GetObjectPropertyByIdx(uint32_t uPropIdx,
1784d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann                                          CFXJSE_Value* lpPropValue) {
1794d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
1804d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> hObject =
1814d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
1824d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  if (!hObject->IsObject())
1834d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    return false;
1844d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
1854d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> hPropValue = hObject.As<v8::Object>()->Get(uPropIdx);
1864d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  lpPropValue->ForceSetValue(hPropValue);
1874d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  return true;
1884d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann}
1894d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
1904d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannbool CFXJSE_Value::DeleteObjectProperty(const CFX_ByteStringC& szPropName) {
1914d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
1924d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> hObject =
1934d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
1944d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  if (!hObject->IsObject())
1954d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    return false;
1964d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
1974d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  hObject.As<v8::Object>()->Delete(v8::String::NewFromUtf8(
1984d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      m_pIsolate, szPropName.c_str(), v8::String::kNormalString,
1994d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      szPropName.GetLength()));
2004d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  return true;
2014d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann}
2024d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
2034d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannbool CFXJSE_Value::HasObjectOwnProperty(const CFX_ByteStringC& szPropName,
2044d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann                                        bool bUseTypeGetter) {
2054d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
2064d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> hObject =
2074d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
2084d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  if (!hObject->IsObject())
2094d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    return false;
2104d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
2114d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::String> hKey = v8::String::NewFromUtf8(
2124d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      m_pIsolate, szPropName.c_str(), v8::String::kNormalString,
2134d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      szPropName.GetLength());
2144d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  return hObject.As<v8::Object>()->HasRealNamedProperty(hKey) ||
2154d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann         (bUseTypeGetter &&
2164d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann          hObject.As<v8::Object>()
2174d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann              ->HasOwnProperty(m_pIsolate->GetCurrentContext(), hKey)
2184d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann              .FromMaybe(false));
2194d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann}
2204d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
2214d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannbool CFXJSE_Value::SetObjectOwnProperty(const CFX_ByteStringC& szPropName,
2224d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann                                        CFXJSE_Value* lpPropValue) {
2234d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  ASSERT(lpPropValue);
2244d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
2254d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> hObject =
2264d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
2274d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  if (!hObject->IsObject())
2284d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    return false;
2294d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
2304d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> pValue =
2314d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      v8::Local<v8::Value>::New(m_pIsolate, lpPropValue->m_hValue);
2324d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  return hObject.As<v8::Object>()
2334d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      ->DefineOwnProperty(
2344d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann          m_pIsolate->GetCurrentContext(),
2354d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann          v8::String::NewFromUtf8(m_pIsolate, szPropName.c_str(),
2364d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann                                  v8::String::kNormalString,
2374d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann                                  szPropName.GetLength()),
2384d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann          pValue)
2394d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      .FromMaybe(false);
2404d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann}
2414d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
2424d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannbool CFXJSE_Value::SetFunctionBind(CFXJSE_Value* lpOldFunction,
2434d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann                                   CFXJSE_Value* lpNewThis) {
2444d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  ASSERT(lpOldFunction && lpNewThis);
2454d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
2464d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
2474d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> rgArgs[2];
2484d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> hOldFunction =
2494d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      v8::Local<v8::Value>::New(m_pIsolate, lpOldFunction->DirectGetValue());
2504d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  if (hOldFunction.IsEmpty() || !hOldFunction->IsFunction())
2514d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    return false;
2524d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
2534d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  rgArgs[0] = hOldFunction;
2544d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> hNewThis =
2554d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      v8::Local<v8::Value>::New(m_pIsolate, lpNewThis->DirectGetValue());
2564d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  if (hNewThis.IsEmpty())
2574d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    return false;
2584d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
2594d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  rgArgs[1] = hNewThis;
2604d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::String> hBinderFuncSource =
2614d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      v8::String::NewFromUtf8(m_pIsolate,
2624d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann                              "(function (oldfunction, newthis) { return "
2634d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann                              "oldfunction.bind(newthis); })");
2644d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Function> hBinderFunc =
2654d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      v8::Script::Compile(hBinderFuncSource)->Run().As<v8::Function>();
2664d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> hBoundFunction =
2674d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      hBinderFunc->Call(m_pIsolate->GetCurrentContext()->Global(), 2, rgArgs);
2684d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  if (hBoundFunction.IsEmpty() || !hBoundFunction->IsFunction())
2694d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    return false;
2704d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
2714d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  m_hValue.Reset(m_pIsolate, hBoundFunction);
2724d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  return true;
2734d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann}
2744d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
2754d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann#define FXJSE_INVALID_PTR ((void*)(intptr_t)-1)
2764d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannbool CFXJSE_Value::Call(CFXJSE_Value* lpReceiver,
2774d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann                        CFXJSE_Value* lpRetValue,
2784d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann                        uint32_t nArgCount,
2794d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann                        CFXJSE_Value** lpArgs) {
2804d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
2814d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> hFunctionValue =
2824d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      v8::Local<v8::Value>::New(m_pIsolate, DirectGetValue());
2834d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Object> hFunctionObject =
2844d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      !hFunctionValue.IsEmpty() && hFunctionValue->IsObject()
2854d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann          ? hFunctionValue.As<v8::Object>()
2864d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann          : v8::Local<v8::Object>();
2874d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
2884d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::TryCatch trycatch(m_pIsolate);
2894d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  if (hFunctionObject.IsEmpty() || !hFunctionObject->IsCallable()) {
2904d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    if (lpRetValue)
2914d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      lpRetValue->ForceSetValue(FXJSE_CreateReturnValue(m_pIsolate, trycatch));
2924d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    return false;
2934d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  }
2944d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
2954d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> hReturnValue;
2964d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value>* lpLocalArgs = NULL;
2974d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  if (nArgCount) {
2984d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    lpLocalArgs = FX_Alloc(v8::Local<v8::Value>, nArgCount);
2994d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    for (uint32_t i = 0; i < nArgCount; i++) {
3004d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      new (lpLocalArgs + i) v8::Local<v8::Value>;
3014d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      CFXJSE_Value* lpArg = lpArgs[i];
3024d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      if (lpArg) {
3034d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann        lpLocalArgs[i] =
3044d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann            v8::Local<v8::Value>::New(m_pIsolate, lpArg->DirectGetValue());
3054d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      }
3064d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      if (lpLocalArgs[i].IsEmpty()) {
3074d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann        lpLocalArgs[i] = v8::Undefined(m_pIsolate);
3084d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      }
3094d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    }
3104d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  }
3114d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
3124d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  bool bRetValue = true;
3134d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  if (lpReceiver == FXJSE_INVALID_PTR) {
3144d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    v8::MaybeLocal<v8::Value> maybe_retvalue =
3154d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann        hFunctionObject->CallAsConstructor(m_pIsolate->GetCurrentContext(),
3164d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann                                           nArgCount, lpLocalArgs);
3174d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    hReturnValue = maybe_retvalue.FromMaybe(v8::Local<v8::Value>());
3184d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  } else {
3194d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    v8::Local<v8::Value> hReceiver;
3204d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    if (lpReceiver) {
3214d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      hReceiver =
3224d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann          v8::Local<v8::Value>::New(m_pIsolate, lpReceiver->DirectGetValue());
3234d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    }
3244d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    if (hReceiver.IsEmpty() || !hReceiver->IsObject())
3254d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      hReceiver = v8::Object::New(m_pIsolate);
3264d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
3274d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    v8::MaybeLocal<v8::Value> maybe_retvalue = hFunctionObject->CallAsFunction(
3284d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann        m_pIsolate->GetCurrentContext(), hReceiver, nArgCount, lpLocalArgs);
3294d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    hReturnValue = maybe_retvalue.FromMaybe(v8::Local<v8::Value>());
3304d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  }
3314d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
3324d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  if (trycatch.HasCaught()) {
3334d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    hReturnValue = FXJSE_CreateReturnValue(m_pIsolate, trycatch);
3344d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    bRetValue = false;
3354d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  }
3364d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
3374d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  if (lpRetValue)
3384d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    lpRetValue->ForceSetValue(hReturnValue);
3394d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
3404d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  if (lpLocalArgs) {
3414d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    for (uint32_t i = 0; i < nArgCount; i++)
3424d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      lpLocalArgs[i].~Local();
3434d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    FX_Free(lpLocalArgs);
3444d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  }
3454d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  return bRetValue;
3464d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann}
3474d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
3484d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannbool CFXJSE_Value::IsUndefined() const {
3494d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  if (m_hValue.IsEmpty())
3504d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    return false;
3514d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
3524d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
3534d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
3544d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  return hValue->IsUndefined();
3554d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann}
3564d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
3574d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannbool CFXJSE_Value::IsNull() const {
3584d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  if (m_hValue.IsEmpty())
3594d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    return false;
3604d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
3614d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
3624d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
3634d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  return hValue->IsNull();
3644d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann}
3654d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
3664d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannbool CFXJSE_Value::IsBoolean() const {
3674d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  if (m_hValue.IsEmpty())
3684d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    return false;
3694d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
3704d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
3714d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
3724d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  return hValue->IsBoolean();
3734d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann}
3744d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
3754d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannbool CFXJSE_Value::IsString() const {
3764d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  if (m_hValue.IsEmpty())
3774d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    return false;
3784d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
3794d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
3804d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
3814d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  return hValue->IsString();
3824d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann}
3834d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
3844d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannbool CFXJSE_Value::IsNumber() const {
3854d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  if (m_hValue.IsEmpty())
3864d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    return false;
3874d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
3884d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
3894d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
3904d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  return hValue->IsNumber();
3914d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann}
3924d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
3934d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannbool CFXJSE_Value::IsInteger() const {
3944d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  if (m_hValue.IsEmpty())
3954d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    return false;
3964d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
3974d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
3984d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
3994d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  return hValue->IsInt32();
4004d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann}
4014d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
4024d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannbool CFXJSE_Value::IsObject() const {
4034d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  if (m_hValue.IsEmpty())
4044d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    return false;
4054d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
4064d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
4074d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
4084d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  return hValue->IsObject();
4094d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann}
4104d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
4114d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannbool CFXJSE_Value::IsArray() const {
4124d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  if (m_hValue.IsEmpty())
4134d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    return false;
4144d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
4154d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
4164d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
4174d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  return hValue->IsArray();
4184d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann}
4194d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
4204d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannbool CFXJSE_Value::IsFunction() const {
4214d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  if (m_hValue.IsEmpty())
4224d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    return false;
4234d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
4244d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
4254d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
4264d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  return hValue->IsFunction();
4274d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann}
4284d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
4294d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannbool CFXJSE_Value::IsDate() const {
4304d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  if (m_hValue.IsEmpty())
4314d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    return false;
4324d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
4334d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
4344d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
4354d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  return hValue->IsDate();
4364d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann}
4374d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
4384d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannbool CFXJSE_Value::ToBoolean() const {
4394d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  ASSERT(!m_hValue.IsEmpty());
4404d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
4414d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
4424d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  return static_cast<bool>(hValue->BooleanValue());
4434d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann}
4444d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
4454d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. MoltmannFX_FLOAT CFXJSE_Value::ToFloat() const {
4464d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  ASSERT(!m_hValue.IsEmpty());
4474d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
4484d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
4494d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  return static_cast<FX_FLOAT>(hValue->NumberValue());
4504d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann}
4514d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
4524d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmanndouble CFXJSE_Value::ToDouble() const {
4534d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  ASSERT(!m_hValue.IsEmpty());
4544d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
4554d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
4564d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  return static_cast<double>(hValue->NumberValue());
4574d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann}
4584d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
4594d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannint32_t CFXJSE_Value::ToInteger() const {
4604d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  ASSERT(!m_hValue.IsEmpty());
4614d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
4624d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
4634d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  return static_cast<int32_t>(hValue->NumberValue());
4644d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann}
4654d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
4664d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. MoltmannCFX_ByteString CFXJSE_Value::ToString() const {
4674d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  ASSERT(!m_hValue.IsEmpty());
4684d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
4694d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
4704d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::String> hString = hValue->ToString();
4714d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::String::Utf8Value hStringVal(hString);
4724d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  return CFX_ByteString(*hStringVal);
4734d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann}
4744d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
4754d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannvoid CFXJSE_Value::SetUndefined() {
4764d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
4774d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> hValue = v8::Undefined(m_pIsolate);
4784d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  m_hValue.Reset(m_pIsolate, hValue);
4794d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann}
4804d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
4814d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannvoid CFXJSE_Value::SetNull() {
4824d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
4834d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> hValue = v8::Null(m_pIsolate);
4844d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  m_hValue.Reset(m_pIsolate, hValue);
4854d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann}
4864d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
4874d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannvoid CFXJSE_Value::SetBoolean(bool bBoolean) {
4884d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
4894d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> hValue = v8::Boolean::New(m_pIsolate, bBoolean != false);
4904d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  m_hValue.Reset(m_pIsolate, hValue);
4914d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann}
4924d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
4934d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannvoid CFXJSE_Value::SetInteger(int32_t nInteger) {
4944d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
4954d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> hValue = v8::Integer::New(m_pIsolate, nInteger);
4964d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  m_hValue.Reset(m_pIsolate, hValue);
4974d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann}
4984d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
4994d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannvoid CFXJSE_Value::SetDouble(double dDouble) {
5004d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
5014d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> hValue = v8::Number::New(m_pIsolate, dDouble);
5024d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  m_hValue.Reset(m_pIsolate, hValue);
5034d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann}
5044d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
5054d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannvoid CFXJSE_Value::SetString(const CFX_ByteStringC& szString) {
5064d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
5074d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> hValue = v8::String::NewFromUtf8(
5084d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      m_pIsolate, reinterpret_cast<const char*>(szString.raw_str()),
5094d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      v8::String::kNormalString, szString.GetLength());
5104d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  m_hValue.Reset(m_pIsolate, hValue);
5114d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann}
5124d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann
5134d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannvoid CFXJSE_Value::SetJSObject() {
5144d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
5154d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  v8::Local<v8::Value> hValue = v8::Object::New(m_pIsolate);
5164d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  m_hValue.Reset(m_pIsolate, hValue);
5174d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann}
518