2008-09-03 00:26:10 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2008 Jacek Caban for CodeWeavers
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2008-10-16 01:49:59 +02:00
|
|
|
#include "config.h"
|
|
|
|
#include "wine/port.h"
|
|
|
|
|
2008-09-19 00:43:03 +02:00
|
|
|
#include <math.h>
|
2012-03-12 12:12:40 +01:00
|
|
|
#include <assert.h>
|
2008-09-19 00:43:03 +02:00
|
|
|
|
2008-09-03 00:26:10 +02:00
|
|
|
#include "jscript.h"
|
2008-09-15 20:40:34 +02:00
|
|
|
#include "engine.h"
|
2008-09-03 00:26:10 +02:00
|
|
|
|
|
|
|
#include "wine/debug.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(jscript);
|
2009-02-25 01:29:32 +01:00
|
|
|
WINE_DECLARE_DEBUG_CHANNEL(heap);
|
2008-09-03 00:26:10 +02:00
|
|
|
|
2012-09-17 15:16:42 +02:00
|
|
|
const char *debugstr_jsval(const jsval_t v)
|
|
|
|
{
|
2012-09-17 15:20:57 +02:00
|
|
|
switch(jsval_type(v)) {
|
2012-09-17 15:16:42 +02:00
|
|
|
case JSV_UNDEFINED:
|
|
|
|
return "undefined";
|
|
|
|
case JSV_NULL:
|
|
|
|
return "null";
|
|
|
|
case JSV_OBJECT:
|
|
|
|
return wine_dbg_sprintf("obj(%p)", get_object(v));
|
|
|
|
case JSV_STRING:
|
2012-10-11 12:16:01 +02:00
|
|
|
return wine_dbg_sprintf("str(%s)", debugstr_jsstr(get_string(v)));
|
2012-09-17 15:16:42 +02:00
|
|
|
case JSV_NUMBER:
|
|
|
|
return wine_dbg_sprintf("%lf", get_number(v));
|
|
|
|
case JSV_BOOL:
|
|
|
|
return get_bool(v) ? "true" : "false";
|
|
|
|
case JSV_VARIANT:
|
|
|
|
return debugstr_variant(get_variant(v));
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(0);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-01-27 20:43:11 +01:00
|
|
|
BOOL is_finite(double n)
|
|
|
|
{
|
|
|
|
return !isnan(n) && !isinf(n);
|
|
|
|
}
|
|
|
|
|
2008-09-03 00:26:10 +02:00
|
|
|
#define MIN_BLOCK_SIZE 128
|
2009-02-25 01:29:32 +01:00
|
|
|
#define ARENA_FREE_FILLER 0xaa
|
2008-09-03 00:26:10 +02:00
|
|
|
|
|
|
|
static inline DWORD block_size(DWORD block)
|
|
|
|
{
|
|
|
|
return MIN_BLOCK_SIZE << block;
|
|
|
|
}
|
|
|
|
|
2013-02-15 14:04:14 +01:00
|
|
|
void heap_pool_init(heap_pool_t *heap)
|
2008-09-03 00:26:10 +02:00
|
|
|
{
|
|
|
|
memset(heap, 0, sizeof(*heap));
|
|
|
|
list_init(&heap->custom_blocks);
|
|
|
|
}
|
|
|
|
|
2013-02-15 14:04:14 +01:00
|
|
|
void *heap_pool_alloc(heap_pool_t *heap, DWORD size)
|
2008-09-03 00:26:10 +02:00
|
|
|
{
|
|
|
|
struct list *list;
|
|
|
|
void *tmp;
|
|
|
|
|
|
|
|
if(!heap->block_cnt) {
|
|
|
|
if(!heap->blocks) {
|
|
|
|
heap->blocks = heap_alloc(sizeof(void*));
|
|
|
|
if(!heap->blocks)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp = heap_alloc(block_size(0));
|
|
|
|
if(!tmp)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
heap->blocks[0] = tmp;
|
|
|
|
heap->block_cnt = 1;
|
|
|
|
}
|
|
|
|
|
2009-02-25 01:28:49 +01:00
|
|
|
if(heap->offset + size <= block_size(heap->last_block)) {
|
2008-09-03 00:26:10 +02:00
|
|
|
tmp = ((BYTE*)heap->blocks[heap->last_block])+heap->offset;
|
|
|
|
heap->offset += size;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2009-02-25 01:28:49 +01:00
|
|
|
if(size <= block_size(heap->last_block+1)) {
|
2008-09-03 00:26:10 +02:00
|
|
|
if(heap->last_block+1 == heap->block_cnt) {
|
|
|
|
tmp = heap_realloc(heap->blocks, (heap->block_cnt+1)*sizeof(void*));
|
|
|
|
if(!tmp)
|
|
|
|
return NULL;
|
|
|
|
|
2009-02-25 01:28:49 +01:00
|
|
|
heap->blocks = tmp;
|
|
|
|
heap->blocks[heap->block_cnt] = heap_alloc(block_size(heap->block_cnt));
|
|
|
|
if(!heap->blocks[heap->block_cnt])
|
|
|
|
return NULL;
|
2008-09-03 00:26:10 +02:00
|
|
|
|
2009-02-25 01:28:49 +01:00
|
|
|
heap->block_cnt++;
|
|
|
|
}
|
2008-09-03 00:26:10 +02:00
|
|
|
|
|
|
|
heap->last_block++;
|
|
|
|
heap->offset = size;
|
|
|
|
return heap->blocks[heap->last_block];
|
|
|
|
}
|
|
|
|
|
|
|
|
list = heap_alloc(size + sizeof(struct list));
|
|
|
|
if(!list)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
list_add_head(&heap->custom_blocks, list);
|
|
|
|
return list+1;
|
|
|
|
}
|
|
|
|
|
2013-02-15 14:04:14 +01:00
|
|
|
void *heap_pool_grow(heap_pool_t *heap, void *mem, DWORD size, DWORD inc)
|
2008-09-15 20:38:46 +02:00
|
|
|
{
|
2012-03-30 18:19:48 +02:00
|
|
|
void *ret;
|
|
|
|
|
2008-09-15 20:38:46 +02:00
|
|
|
if(mem == (BYTE*)heap->blocks[heap->last_block] + heap->offset-size
|
|
|
|
&& heap->offset+inc < block_size(heap->last_block)) {
|
|
|
|
heap->offset += inc;
|
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
2013-02-15 14:04:14 +01:00
|
|
|
ret = heap_pool_alloc(heap, size+inc);
|
2012-04-17 22:09:00 +02:00
|
|
|
if(ret) /* FIXME: avoid copying for custom blocks */
|
2012-03-30 18:19:48 +02:00
|
|
|
memcpy(ret, mem, size);
|
|
|
|
return ret;
|
2008-09-15 20:38:46 +02:00
|
|
|
}
|
|
|
|
|
2013-02-15 14:04:14 +01:00
|
|
|
void heap_pool_clear(heap_pool_t *heap)
|
2008-09-03 00:26:10 +02:00
|
|
|
{
|
|
|
|
struct list *tmp;
|
|
|
|
|
2008-09-15 20:38:46 +02:00
|
|
|
if(!heap)
|
|
|
|
return;
|
|
|
|
|
2016-07-07 05:40:55 +02:00
|
|
|
while((tmp = list_head(&heap->custom_blocks))) {
|
2008-09-03 00:26:10 +02:00
|
|
|
list_remove(tmp);
|
|
|
|
heap_free(tmp);
|
|
|
|
}
|
2008-09-15 20:38:46 +02:00
|
|
|
|
2009-02-25 01:29:32 +01:00
|
|
|
if(WARN_ON(heap)) {
|
|
|
|
DWORD i;
|
|
|
|
|
|
|
|
for(i=0; i < heap->block_cnt; i++)
|
|
|
|
memset(heap->blocks[i], ARENA_FREE_FILLER, block_size(i));
|
|
|
|
}
|
|
|
|
|
2008-09-15 20:38:46 +02:00
|
|
|
heap->last_block = heap->offset = 0;
|
2009-02-25 01:29:02 +01:00
|
|
|
heap->mark = FALSE;
|
2008-09-03 00:26:10 +02:00
|
|
|
}
|
|
|
|
|
2013-02-15 14:04:14 +01:00
|
|
|
void heap_pool_free(heap_pool_t *heap)
|
2008-09-03 00:26:10 +02:00
|
|
|
{
|
|
|
|
DWORD i;
|
|
|
|
|
2013-02-15 14:04:14 +01:00
|
|
|
heap_pool_clear(heap);
|
2008-09-03 00:26:10 +02:00
|
|
|
|
|
|
|
for(i=0; i < heap->block_cnt; i++)
|
|
|
|
heap_free(heap->blocks[i]);
|
|
|
|
heap_free(heap->blocks);
|
|
|
|
|
2013-02-15 14:04:14 +01:00
|
|
|
heap_pool_init(heap);
|
2008-09-03 00:26:10 +02:00
|
|
|
}
|
2008-09-09 01:25:40 +02:00
|
|
|
|
2013-02-15 14:04:14 +01:00
|
|
|
heap_pool_t *heap_pool_mark(heap_pool_t *heap)
|
2008-09-15 20:38:46 +02:00
|
|
|
{
|
|
|
|
if(heap->mark)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
heap->mark = TRUE;
|
|
|
|
return heap;
|
|
|
|
}
|
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
void jsval_release(jsval_t val)
|
|
|
|
{
|
2012-09-17 15:20:57 +02:00
|
|
|
switch(jsval_type(val)) {
|
2012-09-17 15:16:20 +02:00
|
|
|
case JSV_OBJECT:
|
2012-09-17 15:21:26 +02:00
|
|
|
if(get_object(val))
|
|
|
|
IDispatch_Release(get_object(val));
|
2012-09-17 15:16:20 +02:00
|
|
|
break;
|
|
|
|
case JSV_STRING:
|
2012-10-11 12:16:01 +02:00
|
|
|
jsstr_release(get_string(val));
|
2012-09-17 15:16:20 +02:00
|
|
|
break;
|
|
|
|
case JSV_VARIANT:
|
2012-09-17 15:21:26 +02:00
|
|
|
VariantClear(get_variant(val));
|
|
|
|
heap_free(get_variant(val));
|
2012-09-17 15:16:20 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-19 20:11:53 +02:00
|
|
|
static HRESULT jsval_variant(jsval_t *val, VARIANT *var)
|
2012-09-17 15:16:20 +02:00
|
|
|
{
|
2012-09-17 15:21:26 +02:00
|
|
|
VARIANT *v;
|
2012-09-17 15:16:20 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
2012-09-18 19:02:27 +02:00
|
|
|
__JSVAL_TYPE(*val) = JSV_VARIANT;
|
|
|
|
__JSVAL_VAR(*val) = v = heap_alloc(sizeof(VARIANT));
|
2016-07-28 18:45:13 +02:00
|
|
|
if(!v) {
|
|
|
|
*val = jsval_undefined();
|
2012-09-17 15:16:20 +02:00
|
|
|
return E_OUTOFMEMORY;
|
2016-07-28 18:45:13 +02:00
|
|
|
}
|
2012-09-17 15:16:20 +02:00
|
|
|
|
2012-09-17 15:21:26 +02:00
|
|
|
V_VT(v) = VT_EMPTY;
|
|
|
|
hres = VariantCopy(v, var);
|
2016-07-28 18:45:13 +02:00
|
|
|
if(FAILED(hres)) {
|
|
|
|
*val = jsval_undefined();
|
2012-09-17 15:21:26 +02:00
|
|
|
heap_free(v);
|
2016-07-28 18:45:13 +02:00
|
|
|
}
|
2012-09-17 15:16:20 +02:00
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
2012-09-17 15:16:42 +02:00
|
|
|
HRESULT jsval_copy(jsval_t v, jsval_t *r)
|
|
|
|
{
|
2012-09-17 15:20:57 +02:00
|
|
|
switch(jsval_type(v)) {
|
2012-09-17 15:16:42 +02:00
|
|
|
case JSV_UNDEFINED:
|
|
|
|
case JSV_NULL:
|
|
|
|
case JSV_NUMBER:
|
|
|
|
case JSV_BOOL:
|
|
|
|
*r = v;
|
|
|
|
return S_OK;
|
|
|
|
case JSV_OBJECT:
|
2012-09-17 15:17:24 +02:00
|
|
|
if(get_object(v))
|
|
|
|
IDispatch_AddRef(get_object(v));
|
2012-09-17 15:16:42 +02:00
|
|
|
*r = v;
|
|
|
|
return S_OK;
|
|
|
|
case JSV_STRING: {
|
2012-10-11 12:16:01 +02:00
|
|
|
jsstr_addref(get_string(v));
|
|
|
|
*r = v;
|
2012-09-17 15:16:42 +02:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
case JSV_VARIANT:
|
|
|
|
return jsval_variant(r, get_variant(v));
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(0);
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
HRESULT variant_to_jsval(VARIANT *var, jsval_t *r)
|
|
|
|
{
|
2015-02-06 12:10:00 +01:00
|
|
|
if(V_VT(var) == (VT_VARIANT|VT_BYREF))
|
|
|
|
var = V_VARIANTREF(var);
|
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
switch(V_VT(var)) {
|
|
|
|
case VT_EMPTY:
|
|
|
|
*r = jsval_undefined();
|
|
|
|
return S_OK;
|
|
|
|
case VT_NULL:
|
|
|
|
*r = jsval_null();
|
|
|
|
return S_OK;
|
|
|
|
case VT_BOOL:
|
|
|
|
*r = jsval_bool(V_BOOL(var));
|
|
|
|
return S_OK;
|
|
|
|
case VT_I4:
|
|
|
|
*r = jsval_number(V_I4(var));
|
|
|
|
return S_OK;
|
|
|
|
case VT_R8:
|
|
|
|
*r = jsval_number(V_R8(var));
|
|
|
|
return S_OK;
|
|
|
|
case VT_BSTR: {
|
2012-10-11 12:16:01 +02:00
|
|
|
jsstr_t *str;
|
|
|
|
|
2013-04-24 11:32:33 +02:00
|
|
|
if(V_BSTR(var)) {
|
|
|
|
str = jsstr_alloc_len(V_BSTR(var), SysStringLen(V_BSTR(var)));
|
|
|
|
if(!str)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}else {
|
|
|
|
str = jsstr_null_bstr();
|
|
|
|
}
|
2012-09-17 15:16:42 +02:00
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
*r = jsval_string(str);
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
case VT_DISPATCH: {
|
2012-09-17 15:16:42 +02:00
|
|
|
if(V_DISPATCH(var))
|
|
|
|
IDispatch_AddRef(V_DISPATCH(var));
|
2012-09-17 15:16:20 +02:00
|
|
|
*r = jsval_disp(V_DISPATCH(var));
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
case VT_I2:
|
2012-09-17 15:16:42 +02:00
|
|
|
*r = jsval_number(V_I2(var));
|
|
|
|
return S_OK;
|
2017-11-01 16:43:37 +01:00
|
|
|
case VT_UI2:
|
|
|
|
*r = jsval_number(V_UI2(var));
|
|
|
|
return S_OK;
|
2012-09-17 15:16:20 +02:00
|
|
|
case VT_INT:
|
2012-09-17 15:16:42 +02:00
|
|
|
*r = jsval_number(V_INT(var));
|
|
|
|
return S_OK;
|
2015-02-03 19:37:07 +01:00
|
|
|
case VT_UI4:
|
|
|
|
*r = jsval_number(V_UI4(var));
|
|
|
|
return S_OK;
|
2017-12-05 18:24:41 +01:00
|
|
|
case VT_UI8:
|
|
|
|
/*
|
|
|
|
* Native doesn't support VT_UI8 here, but it's needed for IE9+ APIs
|
|
|
|
* (native IE9 doesn't use jscript.dll for JavaScript).
|
|
|
|
*/
|
|
|
|
*r = jsval_number(V_UI8(var));
|
|
|
|
return S_OK;
|
2017-08-21 21:34:39 +02:00
|
|
|
case VT_R4:
|
|
|
|
*r = jsval_number(V_R4(var));
|
|
|
|
return S_OK;
|
2012-12-05 14:49:42 +01:00
|
|
|
case VT_UNKNOWN:
|
|
|
|
if(V_UNKNOWN(var)) {
|
|
|
|
IDispatch *disp;
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
hres = IUnknown_QueryInterface(V_UNKNOWN(var), &IID_IDispatch, (void**)&disp);
|
|
|
|
if(SUCCEEDED(hres)) {
|
|
|
|
*r = jsval_disp(disp);
|
|
|
|
return S_OK;
|
|
|
|
}
|
2014-03-19 13:15:14 +01:00
|
|
|
}else {
|
|
|
|
*r = jsval_disp(NULL);
|
|
|
|
return S_OK;
|
2012-12-05 14:49:42 +01:00
|
|
|
}
|
|
|
|
/* fall through */
|
2012-09-17 15:16:20 +02:00
|
|
|
default:
|
|
|
|
return jsval_variant(r, var);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT jsval_to_variant(jsval_t val, VARIANT *retv)
|
|
|
|
{
|
2012-09-17 15:20:57 +02:00
|
|
|
switch(jsval_type(val)) {
|
2012-09-17 15:16:20 +02:00
|
|
|
case JSV_UNDEFINED:
|
|
|
|
V_VT(retv) = VT_EMPTY;
|
|
|
|
return S_OK;
|
|
|
|
case JSV_NULL:
|
|
|
|
V_VT(retv) = VT_NULL;
|
|
|
|
return S_OK;
|
|
|
|
case JSV_OBJECT:
|
|
|
|
V_VT(retv) = VT_DISPATCH;
|
2012-09-17 15:21:26 +02:00
|
|
|
if(get_object(val))
|
|
|
|
IDispatch_AddRef(get_object(val));
|
|
|
|
V_DISPATCH(retv) = get_object(val);
|
2012-09-17 15:16:20 +02:00
|
|
|
return S_OK;
|
2012-10-11 12:16:01 +02:00
|
|
|
case JSV_STRING: {
|
|
|
|
jsstr_t *str = get_string(val);
|
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
V_VT(retv) = VT_BSTR;
|
2013-04-24 11:32:33 +02:00
|
|
|
if(is_null_bstr(str)) {
|
2012-10-11 12:16:01 +02:00
|
|
|
V_BSTR(retv) = NULL;
|
|
|
|
}else {
|
2013-03-05 11:49:48 +01:00
|
|
|
V_BSTR(retv) = SysAllocStringLen(NULL, jsstr_length(str));
|
|
|
|
if(V_BSTR(retv))
|
|
|
|
jsstr_flush(str, V_BSTR(retv));
|
|
|
|
else
|
2012-09-17 15:16:42 +02:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
2012-09-17 15:16:20 +02:00
|
|
|
return S_OK;
|
2012-10-11 12:16:01 +02:00
|
|
|
}
|
2012-09-17 15:19:44 +02:00
|
|
|
case JSV_NUMBER: {
|
|
|
|
double n = get_number(val);
|
|
|
|
|
|
|
|
if(is_int32(n)) {
|
|
|
|
V_VT(retv) = VT_I4;
|
|
|
|
V_I4(retv) = n;
|
|
|
|
}else {
|
|
|
|
V_VT(retv) = VT_R8;
|
|
|
|
V_R8(retv) = n;
|
|
|
|
}
|
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
return S_OK;
|
2012-09-17 15:19:44 +02:00
|
|
|
}
|
2012-09-17 15:16:20 +02:00
|
|
|
case JSV_BOOL:
|
|
|
|
V_VT(retv) = VT_BOOL;
|
2012-09-17 15:21:26 +02:00
|
|
|
V_BOOL(retv) = get_bool(val) ? VARIANT_TRUE : VARIANT_FALSE;
|
2012-09-17 15:16:20 +02:00
|
|
|
return S_OK;
|
|
|
|
case JSV_VARIANT:
|
2012-09-17 15:17:02 +02:00
|
|
|
V_VT(retv) = VT_EMPTY;
|
2012-09-17 15:21:26 +02:00
|
|
|
return VariantCopy(retv, get_variant(val));
|
2012-09-17 15:16:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(0);
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
2008-09-10 21:12:45 +02:00
|
|
|
/* ECMA-262 3rd Edition 9.1 */
|
2012-09-18 19:01:49 +02:00
|
|
|
HRESULT to_primitive(script_ctx_t *ctx, jsval_t val, jsval_t *ret, hint_t hint)
|
2008-09-10 21:12:45 +02:00
|
|
|
{
|
2012-09-17 15:17:50 +02:00
|
|
|
if(is_object_instance(val)) {
|
2010-09-06 16:11:49 +02:00
|
|
|
jsdisp_t *jsdisp;
|
2012-09-17 15:17:50 +02:00
|
|
|
jsval_t prim;
|
2009-07-08 10:52:56 +02:00
|
|
|
DISPID id;
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
|
|
|
|
static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};
|
|
|
|
|
2012-09-17 15:17:50 +02:00
|
|
|
if(!get_object(val)) {
|
|
|
|
*ret = jsval_null();
|
|
|
|
return S_OK;
|
2009-09-11 18:47:50 +02:00
|
|
|
}
|
|
|
|
|
2016-07-20 13:40:41 +02:00
|
|
|
jsdisp = iface_to_jsdisp(get_object(val));
|
2012-09-17 15:17:50 +02:00
|
|
|
if(!jsdisp)
|
2012-09-18 19:01:49 +02:00
|
|
|
return disp_propget(ctx, get_object(val), DISPID_VALUE, ret);
|
2009-07-08 10:52:56 +02:00
|
|
|
|
|
|
|
if(hint == NO_HINT)
|
|
|
|
hint = is_class(jsdisp, JSCLASS_DATE) ? HINT_STRING : HINT_NUMBER;
|
|
|
|
|
|
|
|
/* Native implementation doesn't throw TypeErrors, returns strange values */
|
|
|
|
|
|
|
|
hres = jsdisp_get_id(jsdisp, hint == HINT_STRING ? toStringW : valueOfW, 0, &id);
|
|
|
|
if(SUCCEEDED(hres)) {
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = jsdisp_call(jsdisp, id, DISPATCH_METHOD, 0, NULL, &prim);
|
2009-07-08 10:52:56 +02:00
|
|
|
if(FAILED(hres)) {
|
2009-07-22 13:02:24 +02:00
|
|
|
WARN("call error - forwarding exception\n");
|
2009-07-08 10:52:56 +02:00
|
|
|
jsdisp_release(jsdisp);
|
|
|
|
return hres;
|
2012-09-17 15:17:50 +02:00
|
|
|
}else if(!is_object_instance(prim)) {
|
2009-07-08 10:52:56 +02:00
|
|
|
jsdisp_release(jsdisp);
|
2012-09-17 15:17:50 +02:00
|
|
|
*ret = prim;
|
2009-07-08 10:52:56 +02:00
|
|
|
return S_OK;
|
2012-09-17 15:17:50 +02:00
|
|
|
}else {
|
|
|
|
IDispatch_Release(get_object(prim));
|
2009-07-08 10:52:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hres = jsdisp_get_id(jsdisp, hint == HINT_STRING ? valueOfW : toStringW, 0, &id);
|
|
|
|
if(SUCCEEDED(hres)) {
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = jsdisp_call(jsdisp, id, DISPATCH_METHOD, 0, NULL, &prim);
|
2009-07-08 10:52:56 +02:00
|
|
|
if(FAILED(hres)) {
|
2009-07-22 13:02:24 +02:00
|
|
|
WARN("call error - forwarding exception\n");
|
2009-07-08 10:52:56 +02:00
|
|
|
jsdisp_release(jsdisp);
|
|
|
|
return hres;
|
2012-09-17 15:17:50 +02:00
|
|
|
}else if(!is_object_instance(prim)) {
|
2009-07-08 10:52:56 +02:00
|
|
|
jsdisp_release(jsdisp);
|
2012-09-17 15:17:50 +02:00
|
|
|
*ret = prim;
|
2009-07-08 10:52:56 +02:00
|
|
|
return S_OK;
|
2012-09-17 15:17:50 +02:00
|
|
|
}else {
|
|
|
|
IDispatch_Release(get_object(prim));
|
2009-07-08 10:52:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
jsdisp_release(jsdisp);
|
|
|
|
|
2009-07-22 13:02:24 +02:00
|
|
|
WARN("failed\n");
|
2012-09-18 19:01:49 +02:00
|
|
|
return throw_type_error(ctx, JS_E_TO_PRIMITIVE, NULL);
|
2009-07-08 10:52:56 +02:00
|
|
|
}
|
2012-09-17 15:17:02 +02:00
|
|
|
|
|
|
|
return jsval_copy(val, ret);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-09-09 01:25:40 +02:00
|
|
|
/* ECMA-262 3rd Edition 9.2 */
|
2012-09-17 15:18:03 +02:00
|
|
|
HRESULT to_boolean(jsval_t val, BOOL *ret)
|
2008-09-09 01:25:40 +02:00
|
|
|
{
|
2012-09-17 15:20:57 +02:00
|
|
|
switch(jsval_type(val)) {
|
2012-09-17 15:18:03 +02:00
|
|
|
case JSV_UNDEFINED:
|
|
|
|
case JSV_NULL:
|
2012-09-17 15:20:14 +02:00
|
|
|
*ret = FALSE;
|
2012-09-17 15:18:03 +02:00
|
|
|
return S_OK;
|
|
|
|
case JSV_OBJECT:
|
|
|
|
*ret = get_object(val) != NULL;
|
|
|
|
return S_OK;
|
|
|
|
case JSV_STRING:
|
2012-10-11 12:16:01 +02:00
|
|
|
*ret = jsstr_length(get_string(val)) != 0;
|
2012-09-17 15:16:42 +02:00
|
|
|
return S_OK;
|
2012-09-17 15:18:03 +02:00
|
|
|
case JSV_NUMBER:
|
|
|
|
*ret = !isnan(get_number(val)) && get_number(val);
|
|
|
|
return S_OK;
|
|
|
|
case JSV_BOOL:
|
|
|
|
*ret = get_bool(val);
|
|
|
|
return S_OK;
|
|
|
|
case JSV_VARIANT:
|
|
|
|
FIXME("unimplemented for variant %s\n", debugstr_variant(get_variant(val)));
|
|
|
|
return E_NOTIMPL;
|
2012-09-17 15:16:42 +02:00
|
|
|
}
|
|
|
|
|
2012-09-17 15:18:03 +02:00
|
|
|
assert(0);
|
|
|
|
return E_FAIL;
|
2012-09-17 15:16:42 +02:00
|
|
|
}
|
|
|
|
|
2008-09-21 19:47:31 +02:00
|
|
|
static int hex_to_int(WCHAR c)
|
|
|
|
{
|
|
|
|
if('0' <= c && c <= '9')
|
|
|
|
return c-'0';
|
|
|
|
|
|
|
|
if('a' <= c && c <= 'f')
|
|
|
|
return c-'a'+10;
|
|
|
|
|
|
|
|
if('A' <= c && c <= 'F')
|
|
|
|
return c-'A'+10;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ECMA-262 3rd Edition 9.3.1 */
|
2012-10-11 12:16:01 +02:00
|
|
|
static HRESULT str_to_number(jsstr_t *str, double *ret)
|
2008-09-21 19:47:31 +02:00
|
|
|
{
|
2013-03-26 10:38:01 +01:00
|
|
|
const WCHAR *ptr;
|
2008-09-21 19:47:31 +02:00
|
|
|
BOOL neg = FALSE;
|
|
|
|
DOUBLE d = 0.0;
|
|
|
|
|
|
|
|
static const WCHAR infinityW[] = {'I','n','f','i','n','i','t','y'};
|
|
|
|
|
2013-03-26 10:38:01 +01:00
|
|
|
ptr = jsstr_flatten(str);
|
|
|
|
if(!ptr)
|
|
|
|
return E_OUTOFMEMORY;
|
2012-03-28 15:04:28 +02:00
|
|
|
|
2008-09-21 19:47:31 +02:00
|
|
|
while(isspaceW(*ptr))
|
|
|
|
ptr++;
|
|
|
|
|
2008-10-16 21:31:40 +02:00
|
|
|
if(*ptr == '-') {
|
|
|
|
neg = TRUE;
|
|
|
|
ptr++;
|
|
|
|
}else if(*ptr == '+') {
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
2018-07-09 20:59:12 +02:00
|
|
|
if(!strncmpW(ptr, infinityW, ARRAY_SIZE(infinityW))) {
|
|
|
|
ptr += ARRAY_SIZE(infinityW);
|
2008-09-21 19:47:31 +02:00
|
|
|
while(*ptr && isspaceW(*ptr))
|
|
|
|
ptr++;
|
|
|
|
|
2008-10-16 21:31:40 +02:00
|
|
|
if(*ptr)
|
2012-05-29 12:53:55 +02:00
|
|
|
*ret = NAN;
|
2008-10-16 21:31:40 +02:00
|
|
|
else
|
2012-06-20 11:19:11 +02:00
|
|
|
*ret = neg ? -INFINITY : INFINITY;
|
2008-10-16 21:31:40 +02:00
|
|
|
return S_OK;
|
2008-09-21 19:47:31 +02:00
|
|
|
}
|
|
|
|
|
2008-10-16 21:31:40 +02:00
|
|
|
if(*ptr == '0' && ptr[1] == 'x') {
|
2008-09-21 19:47:31 +02:00
|
|
|
DWORD l = 0;
|
|
|
|
|
|
|
|
ptr += 2;
|
|
|
|
while((l = hex_to_int(*ptr)) != -1) {
|
|
|
|
d = d*16 + l;
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
2012-03-27 11:28:51 +02:00
|
|
|
*ret = d;
|
2008-09-21 19:47:31 +02:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(isdigitW(*ptr))
|
|
|
|
d = d*10 + (*ptr++ - '0');
|
|
|
|
|
|
|
|
if(*ptr == 'e' || *ptr == 'E') {
|
|
|
|
BOOL eneg = FALSE;
|
|
|
|
LONG l = 0;
|
|
|
|
|
|
|
|
ptr++;
|
|
|
|
if(*ptr == '-') {
|
|
|
|
ptr++;
|
|
|
|
eneg = TRUE;
|
|
|
|
}else if(*ptr == '+') {
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(isdigitW(*ptr))
|
|
|
|
l = l*10 + (*ptr++ - '0');
|
|
|
|
if(eneg)
|
|
|
|
l = -l;
|
|
|
|
|
|
|
|
d *= pow(10, l);
|
|
|
|
}else if(*ptr == '.') {
|
|
|
|
DOUBLE dec = 0.1;
|
|
|
|
|
|
|
|
ptr++;
|
|
|
|
while(isdigitW(*ptr)) {
|
|
|
|
d += dec * (*ptr++ - '0');
|
|
|
|
dec *= 0.1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while(isspaceW(*ptr))
|
|
|
|
ptr++;
|
|
|
|
|
|
|
|
if(*ptr) {
|
2012-05-29 12:53:55 +02:00
|
|
|
*ret = NAN;
|
2008-10-16 21:31:40 +02:00
|
|
|
return S_OK;
|
2008-09-21 19:47:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(neg)
|
|
|
|
d = -d;
|
|
|
|
|
2012-03-27 11:28:51 +02:00
|
|
|
*ret = d;
|
2008-09-21 19:47:31 +02:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2008-09-10 21:12:45 +02:00
|
|
|
/* ECMA-262 3rd Edition 9.3 */
|
2012-09-18 19:01:49 +02:00
|
|
|
HRESULT to_number(script_ctx_t *ctx, jsval_t val, double *ret)
|
2008-09-10 21:12:45 +02:00
|
|
|
{
|
2012-09-17 15:20:57 +02:00
|
|
|
switch(jsval_type(val)) {
|
2012-09-17 15:18:20 +02:00
|
|
|
case JSV_UNDEFINED:
|
2012-05-29 12:53:55 +02:00
|
|
|
*ret = NAN;
|
2012-09-17 15:18:20 +02:00
|
|
|
return S_OK;
|
|
|
|
case JSV_NULL:
|
2012-03-27 11:28:51 +02:00
|
|
|
*ret = 0;
|
2012-09-17 15:18:20 +02:00
|
|
|
return S_OK;
|
|
|
|
case JSV_NUMBER:
|
|
|
|
*ret = get_number(val);
|
|
|
|
return S_OK;
|
|
|
|
case JSV_STRING:
|
|
|
|
return str_to_number(get_string(val), ret);
|
|
|
|
case JSV_OBJECT: {
|
2012-09-17 15:17:50 +02:00
|
|
|
jsval_t prim;
|
2008-09-25 00:52:10 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_primitive(ctx, val, &prim, HINT_NUMBER);
|
2008-09-25 00:52:10 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_number(ctx, prim, ret);
|
2012-09-17 15:17:50 +02:00
|
|
|
jsval_release(prim);
|
2008-09-25 00:52:10 +02:00
|
|
|
return hres;
|
|
|
|
}
|
2012-09-17 15:18:20 +02:00
|
|
|
case JSV_BOOL:
|
|
|
|
*ret = get_bool(val) ? 1 : 0;
|
2012-09-17 15:16:20 +02:00
|
|
|
return S_OK;
|
2012-09-17 15:18:20 +02:00
|
|
|
case JSV_VARIANT:
|
|
|
|
FIXME("unimplemented for variant %s\n", debugstr_variant(get_variant(val)));
|
|
|
|
return E_NOTIMPL;
|
|
|
|
};
|
2012-09-17 15:16:20 +02:00
|
|
|
|
2012-09-17 15:18:20 +02:00
|
|
|
assert(0);
|
|
|
|
return E_FAIL;
|
2012-09-17 15:16:20 +02:00
|
|
|
}
|
|
|
|
|
2008-09-19 00:43:03 +02:00
|
|
|
/* ECMA-262 3rd Edition 9.4 */
|
2012-09-18 19:01:49 +02:00
|
|
|
HRESULT to_integer(script_ctx_t *ctx, jsval_t v, double *ret)
|
2008-09-19 00:43:03 +02:00
|
|
|
{
|
2012-03-27 11:28:51 +02:00
|
|
|
double n;
|
2008-09-19 00:43:03 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_number(ctx, v, &n);
|
2008-09-19 00:43:03 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-05-03 10:42:42 +02:00
|
|
|
if(isnan(n))
|
|
|
|
*ret = 0;
|
|
|
|
else
|
|
|
|
*ret = n >= 0.0 ? floor(n) : -floor(-n);
|
2008-09-19 00:43:03 +02:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-17 15:16:42 +02:00
|
|
|
/* ECMA-262 3rd Edition 9.5 */
|
2012-09-18 19:01:49 +02:00
|
|
|
HRESULT to_int32(script_ctx_t *ctx, jsval_t v, INT *ret)
|
2012-09-17 15:16:42 +02:00
|
|
|
{
|
|
|
|
double n;
|
|
|
|
HRESULT hres;
|
|
|
|
|
2018-04-11 19:36:00 +02:00
|
|
|
const double p32 = (double)0xffffffff + 1;
|
|
|
|
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_number(ctx, v, &n);
|
2012-09-17 15:16:42 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2018-04-11 19:36:00 +02:00
|
|
|
if(is_finite(n))
|
|
|
|
n = n > 0 ? fmod(n, p32) : -fmod(-n, p32);
|
|
|
|
else
|
|
|
|
n = 0;
|
|
|
|
|
|
|
|
*ret = (UINT32)n;
|
2012-09-17 15:16:42 +02:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2008-09-19 00:44:11 +02:00
|
|
|
/* ECMA-262 3rd Edition 9.6 */
|
2018-12-18 02:11:41 +01:00
|
|
|
HRESULT to_uint32(script_ctx_t *ctx, jsval_t val, UINT32 *ret)
|
2008-09-19 00:44:11 +02:00
|
|
|
{
|
2013-02-19 12:45:42 +01:00
|
|
|
INT32 n;
|
2008-09-19 00:44:11 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
2013-02-19 12:45:42 +01:00
|
|
|
hres = to_int32(ctx, val, &n);
|
|
|
|
if(SUCCEEDED(hres))
|
|
|
|
*ret = n;
|
|
|
|
return hres;
|
2008-09-19 00:44:11 +02:00
|
|
|
}
|
|
|
|
|
2012-10-11 12:16:01 +02:00
|
|
|
static jsstr_t *int_to_string(int i)
|
2008-09-17 23:34:56 +02:00
|
|
|
{
|
|
|
|
WCHAR buf[12], *p;
|
|
|
|
BOOL neg = FALSE;
|
|
|
|
|
|
|
|
if(!i) {
|
|
|
|
static const WCHAR zeroW[] = {'0',0};
|
2012-10-11 12:16:01 +02:00
|
|
|
return jsstr_alloc(zeroW);
|
2008-09-17 23:34:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(i < 0) {
|
|
|
|
neg = TRUE;
|
|
|
|
i = -i;
|
|
|
|
}
|
|
|
|
|
2018-07-09 20:59:12 +02:00
|
|
|
p = buf + ARRAY_SIZE(buf)-1;
|
2008-09-17 23:34:56 +02:00
|
|
|
*p-- = 0;
|
|
|
|
while(i) {
|
|
|
|
*p-- = i%10 + '0';
|
|
|
|
i /= 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(neg)
|
|
|
|
*p = '-';
|
|
|
|
else
|
|
|
|
p++;
|
|
|
|
|
2012-10-11 12:16:01 +02:00
|
|
|
return jsstr_alloc(p);
|
2008-09-17 23:34:56 +02:00
|
|
|
}
|
|
|
|
|
2012-10-11 12:16:01 +02:00
|
|
|
HRESULT double_to_string(double n, jsstr_t **str)
|
2011-12-16 11:43:31 +01:00
|
|
|
{
|
2018-11-30 08:14:07 +01:00
|
|
|
static const WCHAR InfinityW[] = {'-','I','n','f','i','n','i','t','y',0};
|
2011-12-16 11:43:31 +01:00
|
|
|
|
|
|
|
if(isnan(n)) {
|
2012-10-11 12:16:01 +02:00
|
|
|
*str = jsstr_nan();
|
2011-12-16 11:43:31 +01:00
|
|
|
}else if(isinf(n)) {
|
2012-10-11 12:16:01 +02:00
|
|
|
*str = jsstr_alloc(n<0 ? InfinityW : InfinityW+1);
|
2012-09-17 15:18:59 +02:00
|
|
|
}else if(is_int32(n)) {
|
2012-10-11 12:16:01 +02:00
|
|
|
*str = int_to_string(n);
|
2011-12-16 11:43:31 +01:00
|
|
|
}else {
|
|
|
|
VARIANT strv, v;
|
|
|
|
HRESULT hres;
|
|
|
|
|
2012-10-11 12:16:01 +02:00
|
|
|
/* FIXME: Don't use VariantChangeTypeEx */
|
2011-12-16 11:43:31 +01:00
|
|
|
V_VT(&v) = VT_R8;
|
|
|
|
V_R8(&v) = n;
|
|
|
|
V_VT(&strv) = VT_EMPTY;
|
|
|
|
hres = VariantChangeTypeEx(&strv, &v, MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT), 0, VT_BSTR);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-10-11 12:16:01 +02:00
|
|
|
*str = jsstr_alloc(V_BSTR(&strv));
|
|
|
|
SysFreeString(V_BSTR(&strv));
|
2011-12-16 11:43:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return *str ? S_OK : E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
|
2008-09-10 21:07:06 +02:00
|
|
|
/* ECMA-262 3rd Edition 9.8 */
|
2012-10-11 12:16:01 +02:00
|
|
|
HRESULT to_string(script_ctx_t *ctx, jsval_t val, jsstr_t **str)
|
2008-09-10 21:07:06 +02:00
|
|
|
{
|
2018-11-30 08:14:07 +01:00
|
|
|
static const WCHAR nullW[] = {'n','u','l','l',0};
|
|
|
|
static const WCHAR trueW[] = {'t','r','u','e',0};
|
|
|
|
static const WCHAR falseW[] = {'f','a','l','s','e',0};
|
2008-09-19 00:46:05 +02:00
|
|
|
|
2012-09-17 15:20:57 +02:00
|
|
|
switch(jsval_type(val)) {
|
2012-09-17 15:18:59 +02:00
|
|
|
case JSV_UNDEFINED:
|
2013-03-08 11:37:56 +01:00
|
|
|
*str = jsstr_undefined();
|
|
|
|
return S_OK;
|
2012-09-17 15:18:59 +02:00
|
|
|
case JSV_NULL:
|
2012-10-11 12:16:01 +02:00
|
|
|
*str = jsstr_alloc(nullW);
|
2008-09-19 00:46:05 +02:00
|
|
|
break;
|
2012-09-17 15:18:59 +02:00
|
|
|
case JSV_NUMBER:
|
2012-10-11 12:16:01 +02:00
|
|
|
return double_to_string(get_number(val), str);
|
2012-09-17 15:18:59 +02:00
|
|
|
case JSV_STRING:
|
2012-10-11 12:16:01 +02:00
|
|
|
*str = jsstr_addref(get_string(val));
|
2008-09-17 23:34:56 +02:00
|
|
|
break;
|
2012-09-17 15:18:59 +02:00
|
|
|
case JSV_OBJECT: {
|
2012-09-17 15:17:50 +02:00
|
|
|
jsval_t prim;
|
2008-09-19 00:46:05 +02:00
|
|
|
HRESULT hres;
|
2008-09-10 21:07:06 +02:00
|
|
|
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_primitive(ctx, val, &prim, HINT_STRING);
|
2008-09-19 00:46:05 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_string(ctx, prim, str);
|
2012-09-17 15:17:50 +02:00
|
|
|
jsval_release(prim);
|
2008-09-19 00:46:05 +02:00
|
|
|
return hres;
|
|
|
|
}
|
2012-09-17 15:18:59 +02:00
|
|
|
case JSV_BOOL:
|
2012-10-11 12:16:01 +02:00
|
|
|
*str = jsstr_alloc(get_bool(val) ? trueW : falseW);
|
2008-09-19 00:46:05 +02:00
|
|
|
break;
|
2008-09-10 21:07:06 +02:00
|
|
|
default:
|
2012-09-17 15:18:59 +02:00
|
|
|
FIXME("unsupported %s\n", debugstr_jsval(val));
|
2008-09-17 23:34:56 +02:00
|
|
|
return E_NOTIMPL;
|
2008-09-10 21:07:06 +02:00
|
|
|
}
|
|
|
|
|
2008-09-17 23:34:56 +02:00
|
|
|
return *str ? S_OK : E_OUTOFMEMORY;
|
2008-09-10 21:07:06 +02:00
|
|
|
}
|
|
|
|
|
2013-03-27 11:01:43 +01:00
|
|
|
HRESULT to_flat_string(script_ctx_t *ctx, jsval_t val, jsstr_t **str, const WCHAR **ret_str)
|
|
|
|
{
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
hres = to_string(ctx, val, str);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
*ret_str = jsstr_flatten(*str);
|
|
|
|
if(!*ret_str) {
|
|
|
|
jsstr_release(*str);
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2008-09-09 01:27:18 +02:00
|
|
|
/* ECMA-262 3rd Edition 9.9 */
|
2012-09-17 15:19:19 +02:00
|
|
|
HRESULT to_object(script_ctx_t *ctx, jsval_t val, IDispatch **disp)
|
2008-09-09 01:27:18 +02:00
|
|
|
{
|
2010-09-06 16:11:49 +02:00
|
|
|
jsdisp_t *dispex;
|
2008-09-15 20:40:34 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
2012-09-17 15:20:57 +02:00
|
|
|
switch(jsval_type(val)) {
|
2012-09-17 15:19:19 +02:00
|
|
|
case JSV_STRING:
|
2012-10-11 12:16:01 +02:00
|
|
|
hres = create_string(ctx, get_string(val), &dispex);
|
2008-09-15 20:40:34 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2010-09-07 15:42:01 +02:00
|
|
|
*disp = to_disp(dispex);
|
2008-09-16 20:45:45 +02:00
|
|
|
break;
|
2012-09-17 15:19:19 +02:00
|
|
|
case JSV_NUMBER:
|
|
|
|
hres = create_number(ctx, get_number(val), &dispex);
|
2008-09-16 20:45:45 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2010-09-07 15:42:01 +02:00
|
|
|
*disp = to_disp(dispex);
|
2008-09-15 20:40:34 +02:00
|
|
|
break;
|
2012-09-17 15:19:19 +02:00
|
|
|
case JSV_OBJECT:
|
|
|
|
if(get_object(val)) {
|
|
|
|
*disp = get_object(val);
|
|
|
|
IDispatch_AddRef(*disp);
|
2009-09-24 00:44:30 +02:00
|
|
|
}else {
|
2010-09-06 16:11:49 +02:00
|
|
|
jsdisp_t *obj;
|
2009-09-24 00:44:30 +02:00
|
|
|
|
|
|
|
hres = create_object(ctx, NULL, &obj);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2010-09-07 15:42:01 +02:00
|
|
|
*disp = to_disp(obj);
|
2009-09-24 00:44:30 +02:00
|
|
|
}
|
2008-09-09 01:27:18 +02:00
|
|
|
break;
|
2012-09-17 15:19:19 +02:00
|
|
|
case JSV_BOOL:
|
|
|
|
hres = create_bool(ctx, get_bool(val), &dispex);
|
2008-09-16 20:45:29 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2010-10-18 18:48:25 +02:00
|
|
|
*disp = to_disp(dispex);
|
|
|
|
break;
|
2012-12-21 16:21:03 +01:00
|
|
|
case JSV_UNDEFINED:
|
|
|
|
case JSV_NULL:
|
|
|
|
WARN("object expected\n");
|
|
|
|
return throw_type_error(ctx, JS_E_OBJECT_EXPECTED, NULL);
|
2012-09-17 15:19:19 +02:00
|
|
|
case JSV_VARIANT:
|
|
|
|
switch(V_VT(get_variant(val))) {
|
|
|
|
case VT_ARRAY|VT_VARIANT:
|
|
|
|
hres = create_vbarray(ctx, V_ARRAY(get_variant(val)), &dispex);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
2010-10-18 18:48:25 +02:00
|
|
|
|
2012-09-17 15:19:19 +02:00
|
|
|
*disp = to_disp(dispex);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
FIXME("Unsupported %s\n", debugstr_variant(get_variant(val)));
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
2008-09-16 20:45:29 +02:00
|
|
|
break;
|
2008-09-09 01:27:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
2012-03-08 14:28:16 +01:00
|
|
|
|
2012-03-12 12:12:26 +01:00
|
|
|
HRESULT variant_change_type(script_ctx_t *ctx, VARIANT *dst, VARIANT *src, VARTYPE vt)
|
|
|
|
{
|
2012-09-17 15:18:20 +02:00
|
|
|
jsval_t val;
|
2012-03-12 12:12:26 +01:00
|
|
|
HRESULT hres;
|
|
|
|
|
2012-09-18 19:01:49 +02:00
|
|
|
clear_ei(ctx);
|
2012-09-17 15:18:20 +02:00
|
|
|
hres = variant_to_jsval(src, &val);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-03-12 12:12:26 +01:00
|
|
|
switch(vt) {
|
|
|
|
case VT_I2:
|
|
|
|
case VT_I4: {
|
|
|
|
INT i;
|
|
|
|
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_int32(ctx, val, &i);
|
2012-03-12 12:12:26 +01:00
|
|
|
if(SUCCEEDED(hres)) {
|
|
|
|
if(vt == VT_I4)
|
|
|
|
V_I4(dst) = i;
|
|
|
|
else
|
|
|
|
V_I2(dst) = i;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2012-03-27 11:28:51 +02:00
|
|
|
case VT_R8: {
|
|
|
|
double n;
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_number(ctx, val, &n);
|
2012-03-27 11:28:51 +02:00
|
|
|
if(SUCCEEDED(hres))
|
|
|
|
V_R8(dst) = n;
|
2012-03-12 12:12:26 +01:00
|
|
|
break;
|
2012-03-27 11:28:51 +02:00
|
|
|
}
|
2012-03-12 12:12:26 +01:00
|
|
|
case VT_R4: {
|
2012-03-27 11:28:51 +02:00
|
|
|
double n;
|
2012-03-12 12:12:26 +01:00
|
|
|
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_number(ctx, val, &n);
|
2012-03-12 12:12:26 +01:00
|
|
|
if(SUCCEEDED(hres))
|
2012-03-27 11:28:51 +02:00
|
|
|
V_R4(dst) = n;
|
2012-03-12 12:12:26 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case VT_BOOL: {
|
2012-09-17 15:18:03 +02:00
|
|
|
BOOL b;
|
|
|
|
|
|
|
|
hres = to_boolean(val, &b);
|
2012-03-12 12:12:26 +01:00
|
|
|
if(SUCCEEDED(hres))
|
2012-09-17 15:18:03 +02:00
|
|
|
V_BOOL(dst) = b ? VARIANT_TRUE : VARIANT_FALSE;
|
2012-03-12 12:12:26 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case VT_BSTR: {
|
2012-10-11 12:16:01 +02:00
|
|
|
jsstr_t *str;
|
2012-03-12 12:12:26 +01:00
|
|
|
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_string(ctx, val, &str);
|
2012-10-11 12:16:01 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
break;
|
|
|
|
|
2013-04-24 11:32:33 +02:00
|
|
|
if(is_null_bstr(str)) {
|
2012-10-11 12:16:01 +02:00
|
|
|
V_BSTR(dst) = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-03-05 11:50:03 +01:00
|
|
|
V_BSTR(dst) = SysAllocStringLen(NULL, jsstr_length(str));
|
|
|
|
if(V_BSTR(dst))
|
|
|
|
jsstr_flush(str, V_BSTR(dst));
|
|
|
|
else
|
2012-10-11 12:16:01 +02:00
|
|
|
hres = E_OUTOFMEMORY;
|
2012-03-12 12:12:26 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case VT_EMPTY:
|
|
|
|
hres = V_VT(src) == VT_EMPTY ? S_OK : E_NOTIMPL;
|
|
|
|
break;
|
|
|
|
case VT_NULL:
|
|
|
|
hres = V_VT(src) == VT_NULL ? S_OK : E_NOTIMPL;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
FIXME("vt %d not implemented\n", vt);
|
|
|
|
hres = E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
2012-09-17 15:18:20 +02:00
|
|
|
jsval_release(val);
|
2012-09-18 19:01:49 +02:00
|
|
|
if(FAILED(hres))
|
2012-03-12 12:12:26 +01:00
|
|
|
return hres;
|
|
|
|
|
|
|
|
V_VT(dst) = vt;
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-08 14:28:16 +01:00
|
|
|
static inline JSCaller *impl_from_IServiceProvider(IServiceProvider *iface)
|
|
|
|
{
|
|
|
|
return CONTAINING_RECORD(iface, JSCaller, IServiceProvider_iface);
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI JSCaller_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
|
|
|
|
{
|
|
|
|
JSCaller *This = impl_from_IServiceProvider(iface);
|
|
|
|
|
|
|
|
if(IsEqualGUID(&IID_IUnknown, riid)) {
|
|
|
|
TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
|
|
|
|
*ppv = &This->IServiceProvider_iface;
|
|
|
|
}else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
|
|
|
|
TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
|
|
|
|
*ppv = &This->IServiceProvider_iface;
|
|
|
|
}else {
|
|
|
|
WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
|
|
|
|
*ppv = NULL;
|
|
|
|
return E_NOINTERFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
IUnknown_AddRef((IUnknown*)*ppv);
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ULONG WINAPI JSCaller_AddRef(IServiceProvider *iface)
|
|
|
|
{
|
|
|
|
JSCaller *This = impl_from_IServiceProvider(iface);
|
|
|
|
LONG ref = InterlockedIncrement(&This->ref);
|
|
|
|
|
|
|
|
TRACE("(%p) ref=%d\n", This, ref);
|
|
|
|
|
|
|
|
return ref;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ULONG WINAPI JSCaller_Release(IServiceProvider *iface)
|
|
|
|
{
|
|
|
|
JSCaller *This = impl_from_IServiceProvider(iface);
|
|
|
|
LONG ref = InterlockedIncrement(&This->ref);
|
|
|
|
|
|
|
|
TRACE("(%p) ref=%d\n", This, ref);
|
|
|
|
|
2012-03-12 12:12:40 +01:00
|
|
|
if(!ref) {
|
|
|
|
assert(!This->ctx);
|
2012-03-08 14:28:16 +01:00
|
|
|
heap_free(This);
|
2012-03-12 12:12:40 +01:00
|
|
|
}
|
2012-03-08 14:28:16 +01:00
|
|
|
|
|
|
|
return ref;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI JSCaller_QueryService(IServiceProvider *iface, REFGUID guidService,
|
|
|
|
REFIID riid, void **ppv)
|
|
|
|
{
|
|
|
|
JSCaller *This = impl_from_IServiceProvider(iface);
|
|
|
|
|
2012-03-12 12:12:40 +01:00
|
|
|
if(IsEqualGUID(guidService, &SID_VariantConversion) && This->ctx && This->ctx->active_script) {
|
|
|
|
TRACE("(%p)->(SID_VariantConversion)\n", This);
|
|
|
|
return IActiveScript_QueryInterface(This->ctx->active_script, riid, ppv);
|
|
|
|
}
|
|
|
|
|
2012-03-08 14:28:16 +01:00
|
|
|
FIXME("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
|
|
|
|
|
|
|
|
*ppv = NULL;
|
|
|
|
return E_NOINTERFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const IServiceProviderVtbl ServiceProviderVtbl = {
|
|
|
|
JSCaller_QueryInterface,
|
|
|
|
JSCaller_AddRef,
|
|
|
|
JSCaller_Release,
|
|
|
|
JSCaller_QueryService
|
|
|
|
};
|
|
|
|
|
|
|
|
HRESULT create_jscaller(script_ctx_t *ctx)
|
|
|
|
{
|
|
|
|
JSCaller *ret;
|
|
|
|
|
|
|
|
ret = heap_alloc(sizeof(*ret));
|
|
|
|
if(!ret)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
ret->IServiceProvider_iface.lpVtbl = &ServiceProviderVtbl;
|
|
|
|
ret->ref = 1;
|
2012-03-12 12:12:40 +01:00
|
|
|
ret->ctx = ctx;
|
2012-03-08 14:28:16 +01:00
|
|
|
|
|
|
|
ctx->jscaller = ret;
|
|
|
|
return S_OK;
|
|
|
|
}
|