2008-09-09 01:27:01 +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
|
|
|
|
*/
|
|
|
|
|
2012-06-21 10:47:36 +02:00
|
|
|
#include <assert.h>
|
|
|
|
|
2008-09-09 01:27:01 +02:00
|
|
|
#include "jscript.h"
|
2008-09-09 01:27:57 +02:00
|
|
|
#include "engine.h"
|
2008-09-09 01:27:01 +02:00
|
|
|
|
|
|
|
#include "wine/debug.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(jscript);
|
|
|
|
|
2019-08-14 13:17:51 +02:00
|
|
|
typedef struct _function_vtbl_t function_vtbl_t;
|
|
|
|
|
2008-09-09 01:27:01 +02:00
|
|
|
typedef struct {
|
2010-09-06 16:11:49 +02:00
|
|
|
jsdisp_t dispex;
|
2019-08-14 13:17:51 +02:00
|
|
|
const function_vtbl_t *vtbl;
|
2008-09-09 01:27:01 +02:00
|
|
|
DWORD flags;
|
|
|
|
DWORD length;
|
|
|
|
} FunctionInstance;
|
|
|
|
|
2019-08-14 13:17:51 +02:00
|
|
|
struct _function_vtbl_t {
|
2019-08-14 13:18:09 +02:00
|
|
|
HRESULT (*call)(script_ctx_t*,FunctionInstance*,IDispatch*,unsigned,unsigned,jsval_t*,jsval_t*);
|
2019-08-14 13:17:51 +02:00
|
|
|
HRESULT (*toString)(FunctionInstance*,jsstr_t**);
|
2019-12-12 13:53:55 +01:00
|
|
|
function_code_t* (*get_code)(FunctionInstance*);
|
2019-08-14 13:18:00 +02:00
|
|
|
void (*destructor)(FunctionInstance*);
|
2019-08-14 13:17:51 +02:00
|
|
|
};
|
|
|
|
|
2019-08-14 13:18:30 +02:00
|
|
|
typedef struct {
|
|
|
|
FunctionInstance function;
|
|
|
|
scope_chain_t *scope_chain;
|
|
|
|
bytecode_t *code;
|
|
|
|
function_code_t *func_code;
|
|
|
|
} InterpretedFunction;
|
|
|
|
|
2019-08-14 13:17:51 +02:00
|
|
|
typedef struct {
|
|
|
|
FunctionInstance function;
|
2019-08-14 13:18:21 +02:00
|
|
|
builtin_invoke_t proc;
|
2019-08-14 13:18:39 +02:00
|
|
|
const WCHAR *name;
|
2019-08-14 13:17:51 +02:00
|
|
|
} NativeFunction;
|
|
|
|
|
2019-08-14 13:18:52 +02:00
|
|
|
typedef struct {
|
|
|
|
FunctionInstance function;
|
|
|
|
FunctionInstance *target;
|
|
|
|
IDispatch *this;
|
|
|
|
unsigned argc;
|
|
|
|
jsval_t args[1];
|
|
|
|
} BindFunction;
|
|
|
|
|
2012-11-30 13:01:51 +01:00
|
|
|
typedef struct {
|
|
|
|
jsdisp_t jsdisp;
|
2019-08-14 13:18:30 +02:00
|
|
|
InterpretedFunction *function;
|
2016-08-03 16:28:19 +02:00
|
|
|
jsval_t *buf;
|
|
|
|
call_frame_t *frame;
|
|
|
|
unsigned argc;
|
2012-11-30 13:01:51 +01:00
|
|
|
} ArgumentsInstance;
|
|
|
|
|
2019-08-14 13:18:52 +02:00
|
|
|
static HRESULT create_bind_function(script_ctx_t*,FunctionInstance*,IDispatch*,unsigned,jsval_t*,jsdisp_t**r);
|
|
|
|
|
2015-02-06 12:09:40 +01:00
|
|
|
static inline FunctionInstance *function_from_jsdisp(jsdisp_t *jsdisp)
|
|
|
|
{
|
|
|
|
return CONTAINING_RECORD(jsdisp, FunctionInstance, dispex);
|
|
|
|
}
|
|
|
|
|
2009-09-23 16:18:39 +02:00
|
|
|
static inline FunctionInstance *function_from_vdisp(vdisp_t *vdisp)
|
|
|
|
{
|
2015-02-06 12:09:40 +01:00
|
|
|
return function_from_jsdisp(vdisp->u.jsdisp);
|
2009-09-23 16:18:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline FunctionInstance *function_this(vdisp_t *jsthis)
|
|
|
|
{
|
|
|
|
return is_vclass(jsthis, JSCLASS_FUNCTION) ? function_from_vdisp(jsthis) : NULL;
|
|
|
|
}
|
|
|
|
|
2016-08-03 16:28:19 +02:00
|
|
|
static inline ArgumentsInstance *arguments_from_jsdisp(jsdisp_t *jsdisp)
|
|
|
|
{
|
|
|
|
return CONTAINING_RECORD(jsdisp, ArgumentsInstance, jsdisp);
|
|
|
|
}
|
|
|
|
|
2008-09-10 02:33:15 +02:00
|
|
|
static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
|
|
|
|
|
2008-09-09 01:27:01 +02:00
|
|
|
static const WCHAR lengthW[] = {'l','e','n','g','t','h',0};
|
|
|
|
static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
|
|
|
|
static const WCHAR applyW[] = {'a','p','p','l','y',0};
|
2019-08-14 13:18:52 +02:00
|
|
|
static const WCHAR bindW[] = {'b','i','n','d',0};
|
2008-09-09 01:27:01 +02:00
|
|
|
static const WCHAR callW[] = {'c','a','l','l',0};
|
2010-07-27 11:31:01 +02:00
|
|
|
static const WCHAR argumentsW[] = {'a','r','g','u','m','e','n','t','s',0};
|
2008-09-09 01:27:01 +02:00
|
|
|
|
2012-09-17 15:16:42 +02:00
|
|
|
static HRESULT Arguments_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
|
2012-09-18 19:01:49 +02:00
|
|
|
jsval_t *r)
|
2009-08-29 00:01:57 +02:00
|
|
|
{
|
|
|
|
FIXME("\n");
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
2012-11-30 13:01:51 +01:00
|
|
|
static void Arguments_destructor(jsdisp_t *jsdisp)
|
|
|
|
{
|
2016-09-22 09:56:28 +02:00
|
|
|
ArgumentsInstance *arguments = arguments_from_jsdisp(jsdisp);
|
2012-11-30 13:01:51 +01:00
|
|
|
|
2016-07-28 18:44:43 +02:00
|
|
|
TRACE("(%p)\n", arguments);
|
|
|
|
|
2016-08-03 16:28:19 +02:00
|
|
|
if(arguments->buf) {
|
|
|
|
unsigned i;
|
|
|
|
for(i = 0; i < arguments->argc; i++)
|
|
|
|
jsval_release(arguments->buf[i]);
|
|
|
|
heap_free(arguments->buf);
|
|
|
|
}
|
|
|
|
|
2019-08-14 13:18:30 +02:00
|
|
|
jsdisp_release(&arguments->function->function.dispex);
|
2012-11-30 13:01:51 +01:00
|
|
|
heap_free(arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned Arguments_idx_length(jsdisp_t *jsdisp)
|
|
|
|
{
|
2016-09-22 09:56:28 +02:00
|
|
|
ArgumentsInstance *arguments = arguments_from_jsdisp(jsdisp);
|
2016-08-03 16:28:19 +02:00
|
|
|
return arguments->argc;
|
2012-11-30 13:01:51 +01:00
|
|
|
}
|
|
|
|
|
2016-08-03 16:27:49 +02:00
|
|
|
static jsval_t *get_argument_ref(ArgumentsInstance *arguments, unsigned idx)
|
|
|
|
{
|
2016-08-03 16:28:19 +02:00
|
|
|
if(arguments->buf)
|
|
|
|
return arguments->buf + idx;
|
|
|
|
if(arguments->frame->base_scope->frame || idx >= arguments->frame->function->param_cnt)
|
|
|
|
return arguments->jsdisp.ctx->stack + arguments->frame->arguments_off + idx;
|
|
|
|
return NULL;
|
2016-08-03 16:27:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT Arguments_idx_get(jsdisp_t *jsdisp, unsigned idx, jsval_t *r)
|
2012-11-30 13:01:51 +01:00
|
|
|
{
|
2016-09-22 09:56:28 +02:00
|
|
|
ArgumentsInstance *arguments = arguments_from_jsdisp(jsdisp);
|
2016-08-03 16:27:49 +02:00
|
|
|
jsval_t *ref;
|
2012-11-30 13:01:51 +01:00
|
|
|
|
|
|
|
TRACE("%p[%u]\n", arguments, idx);
|
|
|
|
|
2016-08-03 16:27:49 +02:00
|
|
|
if((ref = get_argument_ref(arguments, idx)))
|
|
|
|
return jsval_copy(*ref, r);
|
2016-07-28 18:44:43 +02:00
|
|
|
|
2012-11-30 13:01:51 +01:00
|
|
|
/* FIXME: Accessing by name won't work for duplicated argument names */
|
2019-08-14 13:18:30 +02:00
|
|
|
return jsdisp_propget_name(arguments->frame->base_scope->jsobj,
|
|
|
|
arguments->function->func_code->params[idx], r);
|
2012-11-30 13:01:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT Arguments_idx_put(jsdisp_t *jsdisp, unsigned idx, jsval_t val)
|
|
|
|
{
|
2016-09-22 09:56:28 +02:00
|
|
|
ArgumentsInstance *arguments = arguments_from_jsdisp(jsdisp);
|
2016-08-03 16:27:49 +02:00
|
|
|
jsval_t *ref;
|
|
|
|
HRESULT hres;
|
2012-11-30 13:01:51 +01:00
|
|
|
|
|
|
|
TRACE("%p[%u] = %s\n", arguments, idx, debugstr_jsval(val));
|
|
|
|
|
2016-08-03 16:27:49 +02:00
|
|
|
if((ref = get_argument_ref(arguments, idx))) {
|
|
|
|
jsval_t copy;
|
|
|
|
hres = jsval_copy(val, ©);
|
2016-07-28 18:44:43 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
2016-08-03 16:27:49 +02:00
|
|
|
|
|
|
|
jsval_release(*ref);
|
|
|
|
*ref = copy;
|
|
|
|
return S_OK;
|
2016-07-28 18:44:43 +02:00
|
|
|
}
|
|
|
|
|
2012-11-30 13:01:51 +01:00
|
|
|
/* FIXME: Accessing by name won't work for duplicated argument names */
|
2019-08-14 13:18:30 +02:00
|
|
|
return jsdisp_propput_name(arguments->frame->base_scope->jsobj,
|
|
|
|
arguments->function->func_code->params[idx], val);
|
2012-11-30 13:01:51 +01:00
|
|
|
}
|
|
|
|
|
2009-08-29 00:01:57 +02:00
|
|
|
static const builtin_info_t Arguments_info = {
|
|
|
|
JSCLASS_ARGUMENTS,
|
|
|
|
{NULL, Arguments_value, 0},
|
|
|
|
0, NULL,
|
2012-11-30 13:01:51 +01:00
|
|
|
Arguments_destructor,
|
2009-08-29 00:01:57 +02:00
|
|
|
NULL,
|
2012-11-30 13:01:51 +01:00
|
|
|
Arguments_idx_length,
|
|
|
|
Arguments_idx_get,
|
|
|
|
Arguments_idx_put
|
2009-08-29 00:01:57 +02:00
|
|
|
};
|
|
|
|
|
2016-08-03 16:28:26 +02:00
|
|
|
HRESULT setup_arguments_object(script_ctx_t *ctx, call_frame_t *frame)
|
2008-09-10 21:06:43 +02:00
|
|
|
{
|
2012-11-30 13:01:51 +01:00
|
|
|
ArgumentsInstance *args;
|
2008-09-10 21:06:43 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
2009-09-20 20:58:27 +02:00
|
|
|
static const WCHAR caleeW[] = {'c','a','l','l','e','e',0};
|
|
|
|
|
2012-11-30 13:01:51 +01:00
|
|
|
args = heap_alloc_zero(sizeof(*args));
|
2009-08-29 00:01:57 +02:00
|
|
|
if(!args)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2012-11-30 13:01:51 +01:00
|
|
|
hres = init_dispex_from_constr(&args->jsdisp, ctx, &Arguments_info, ctx->object_constr);
|
2009-08-29 00:01:57 +02:00
|
|
|
if(FAILED(hres)) {
|
|
|
|
heap_free(args);
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
2019-08-14 13:18:30 +02:00
|
|
|
args->function = (InterpretedFunction*)function_from_jsdisp(jsdisp_addref(frame->function_instance));
|
2016-08-03 16:28:26 +02:00
|
|
|
args->argc = frame->argc;
|
2016-08-03 16:28:19 +02:00
|
|
|
args->frame = frame;
|
2012-11-05 15:04:01 +01:00
|
|
|
|
2018-05-14 13:33:38 +02:00
|
|
|
hres = jsdisp_define_data_property(&args->jsdisp, lengthW, PROPF_WRITABLE | PROPF_CONFIGURABLE,
|
|
|
|
jsval_number(args->argc));
|
2016-08-03 16:28:19 +02:00
|
|
|
if(SUCCEEDED(hres))
|
2018-05-14 13:33:38 +02:00
|
|
|
hres = jsdisp_define_data_property(&args->jsdisp, caleeW, PROPF_WRITABLE | PROPF_CONFIGURABLE,
|
2019-08-14 13:18:30 +02:00
|
|
|
jsval_obj(&args->function->function.dispex));
|
2016-08-03 16:28:19 +02:00
|
|
|
if(SUCCEEDED(hres))
|
2018-05-03 14:58:55 +02:00
|
|
|
hres = jsdisp_propput(frame->base_scope->jsobj, argumentsW, PROPF_WRITABLE, jsval_obj(&args->jsdisp));
|
2009-08-29 00:01:57 +02:00
|
|
|
if(FAILED(hres)) {
|
2012-11-30 13:01:51 +01:00
|
|
|
jsdisp_release(&args->jsdisp);
|
2009-08-29 00:01:57 +02:00
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
2016-08-03 16:28:19 +02:00
|
|
|
frame->arguments_obj = &args->jsdisp;
|
2009-08-29 00:01:57 +02:00
|
|
|
return S_OK;
|
2008-09-10 21:06:43 +02:00
|
|
|
}
|
|
|
|
|
2016-08-03 16:28:19 +02:00
|
|
|
void detach_arguments_object(jsdisp_t *args_disp)
|
|
|
|
{
|
|
|
|
ArgumentsInstance *arguments = arguments_from_jsdisp(args_disp);
|
|
|
|
call_frame_t *frame = arguments->frame;
|
|
|
|
const BOOL on_stack = frame->base_scope->frame == frame;
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
/* Reset arguments value to cut the reference cycle. Note that since all activation contexts have
|
|
|
|
* their own arguments property, it's impossible to use prototype's one during name lookup */
|
|
|
|
jsdisp_propput_name(frame->base_scope->jsobj, argumentsW, jsval_undefined());
|
|
|
|
arguments->frame = NULL;
|
|
|
|
|
|
|
|
/* Don't bother coppying arguments if call frame holds the last reference. */
|
|
|
|
if(arguments->jsdisp.ref > 1) {
|
|
|
|
arguments->buf = heap_alloc(arguments->argc * sizeof(*arguments->buf));
|
|
|
|
if(arguments->buf) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i = 0; i < arguments->argc ; i++) {
|
|
|
|
if(on_stack || i >= frame->function->param_cnt)
|
|
|
|
hres = jsval_copy(arguments->jsdisp.ctx->stack[frame->arguments_off + i], arguments->buf+i);
|
|
|
|
else
|
|
|
|
hres = jsdisp_propget_name(frame->base_scope->jsobj, frame->function->params[i], arguments->buf+i);
|
|
|
|
if(FAILED(hres))
|
|
|
|
arguments->buf[i] = jsval_undefined();
|
|
|
|
}
|
|
|
|
}else {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
arguments->argc = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
jsdisp_release(frame->arguments_obj);
|
|
|
|
}
|
|
|
|
|
2012-09-18 19:01:49 +02:00
|
|
|
HRESULT Function_invoke(jsdisp_t *func_this, IDispatch *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
|
2012-06-21 10:47:36 +02:00
|
|
|
{
|
|
|
|
FunctionInstance *function;
|
|
|
|
|
2012-06-21 10:48:10 +02:00
|
|
|
TRACE("func %p this %p\n", func_this, jsthis);
|
2012-06-21 10:47:36 +02:00
|
|
|
|
|
|
|
assert(is_class(func_this, JSCLASS_FUNCTION));
|
2016-09-22 09:56:28 +02:00
|
|
|
function = function_from_jsdisp(func_this);
|
2012-06-21 10:47:36 +02:00
|
|
|
|
2019-08-14 13:18:09 +02:00
|
|
|
return function->vtbl->call(function->dispex.ctx, function, jsthis, flags, argc, argv, r);
|
2012-06-21 10:47:36 +02:00
|
|
|
}
|
|
|
|
|
2015-02-06 12:09:40 +01:00
|
|
|
static HRESULT Function_get_length(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
|
2008-09-09 01:27:01 +02:00
|
|
|
{
|
2015-02-06 12:09:40 +01:00
|
|
|
TRACE("%p\n", jsthis);
|
2008-09-09 01:27:38 +02:00
|
|
|
|
2015-02-06 12:09:40 +01:00
|
|
|
*r = jsval_number(function_from_jsdisp(jsthis)->length);
|
2008-09-09 01:27:38 +02:00
|
|
|
return S_OK;
|
2008-09-09 01:27:01 +02:00
|
|
|
}
|
|
|
|
|
2012-09-17 15:16:42 +02:00
|
|
|
static HRESULT Function_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
|
2012-09-18 19:01:49 +02:00
|
|
|
jsval_t *r)
|
2008-09-09 01:27:01 +02:00
|
|
|
{
|
2008-09-21 15:47:47 +02:00
|
|
|
FunctionInstance *function;
|
2012-10-11 12:16:01 +02:00
|
|
|
jsstr_t *str;
|
2008-09-21 15:47:47 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
2009-09-23 16:18:39 +02:00
|
|
|
if(!(function = function_this(jsthis)))
|
2012-09-18 19:01:49 +02:00
|
|
|
return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
|
2008-09-21 15:47:47 +02:00
|
|
|
|
2019-08-14 13:17:51 +02:00
|
|
|
hres = function->vtbl->toString(function, &str);
|
2008-09-21 15:47:47 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_string(str);
|
|
|
|
else
|
2012-10-11 12:16:01 +02:00
|
|
|
jsstr_release(str);
|
2008-09-21 15:47:47 +02:00
|
|
|
return S_OK;
|
2008-09-09 01:27:01 +02:00
|
|
|
}
|
|
|
|
|
2012-09-18 19:01:49 +02:00
|
|
|
static HRESULT array_to_args(script_ctx_t *ctx, jsdisp_t *arg_array, unsigned *argc, jsval_t **ret)
|
2009-08-29 00:02:49 +02:00
|
|
|
{
|
2012-09-17 15:16:42 +02:00
|
|
|
jsval_t *argv, val;
|
2009-08-29 00:02:49 +02:00
|
|
|
DWORD length, i;
|
|
|
|
HRESULT hres;
|
|
|
|
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = jsdisp_propget_name(arg_array, lengthW, &val);
|
2009-08-29 00:02:49 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_uint32(ctx, val, &length);
|
2012-09-17 15:16:42 +02:00
|
|
|
jsval_release(val);
|
2009-08-29 00:02:49 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-09-17 15:16:42 +02:00
|
|
|
argv = heap_alloc(length * sizeof(*argv));
|
2009-08-30 23:08:40 +02:00
|
|
|
if(!argv)
|
2009-08-29 00:02:49 +02:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
for(i=0; i<length; i++) {
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = jsdisp_get_idx(arg_array, i, argv+i);
|
2012-09-17 15:16:42 +02:00
|
|
|
if(hres == DISP_E_UNKNOWNNAME) {
|
|
|
|
argv[i] = jsval_undefined();
|
|
|
|
}else if(FAILED(hres)) {
|
2009-08-29 00:02:49 +02:00
|
|
|
while(i--)
|
2012-09-17 15:16:42 +02:00
|
|
|
jsval_release(argv[i]);
|
2009-08-29 00:02:49 +02:00
|
|
|
heap_free(argv);
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-25 14:08:38 +02:00
|
|
|
*argc = length;
|
|
|
|
*ret = argv;
|
2009-08-29 00:02:49 +02:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-18 19:01:49 +02:00
|
|
|
static HRESULT Function_apply(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
|
2008-09-09 01:27:01 +02:00
|
|
|
{
|
2009-08-29 00:02:49 +02:00
|
|
|
FunctionInstance *function;
|
2012-09-17 15:16:42 +02:00
|
|
|
jsval_t *args = NULL;
|
2012-06-25 14:08:38 +02:00
|
|
|
unsigned i, cnt = 0;
|
2009-09-20 19:00:09 +02:00
|
|
|
IDispatch *this_obj = NULL;
|
2009-08-29 00:02:49 +02:00
|
|
|
HRESULT hres = S_OK;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
2016-06-15 18:37:13 +02:00
|
|
|
if(!(function = function_this(jsthis)) && (jsthis->flags & VDISP_JSDISP))
|
2012-09-18 19:01:49 +02:00
|
|
|
return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
|
2009-08-29 00:02:49 +02:00
|
|
|
|
|
|
|
if(argc) {
|
2012-09-17 15:16:42 +02:00
|
|
|
if(!is_undefined(argv[0]) && !is_null(argv[0])) {
|
2012-09-17 15:19:19 +02:00
|
|
|
hres = to_object(ctx, argv[0], &this_obj);
|
2009-10-30 00:02:13 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
}
|
2009-08-29 00:02:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(argc >= 2) {
|
2010-09-06 16:11:49 +02:00
|
|
|
jsdisp_t *arg_array = NULL;
|
2009-08-29 00:02:49 +02:00
|
|
|
|
2012-09-17 15:16:42 +02:00
|
|
|
if(is_object_instance(argv[1])) {
|
2016-07-20 13:40:41 +02:00
|
|
|
arg_array = iface_to_jsdisp(get_object(argv[1]));
|
2009-10-30 00:02:13 +01:00
|
|
|
if(arg_array &&
|
|
|
|
(!is_class(arg_array, JSCLASS_ARRAY) && !is_class(arg_array, JSCLASS_ARGUMENTS) )) {
|
2009-08-29 00:02:49 +02:00
|
|
|
jsdisp_release(arg_array);
|
|
|
|
arg_array = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(arg_array) {
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = array_to_args(ctx, arg_array, &cnt, &args);
|
2009-08-29 00:02:49 +02:00
|
|
|
jsdisp_release(arg_array);
|
|
|
|
}else {
|
2009-08-30 20:19:58 +02:00
|
|
|
FIXME("throw TypeError\n");
|
2009-08-29 00:02:49 +02:00
|
|
|
hres = E_FAIL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-15 18:37:13 +02:00
|
|
|
if(SUCCEEDED(hres)) {
|
|
|
|
if(function) {
|
2019-08-14 13:18:09 +02:00
|
|
|
hres = function->vtbl->call(ctx, function, this_obj, flags, cnt, args, r);
|
2016-06-15 18:37:13 +02:00
|
|
|
}else {
|
|
|
|
jsval_t res;
|
|
|
|
hres = disp_call_value(ctx, jsthis->u.disp, this_obj, DISPATCH_METHOD, cnt, args, &res);
|
|
|
|
if(SUCCEEDED(hres)) {
|
|
|
|
if(r)
|
|
|
|
*r = res;
|
|
|
|
else
|
|
|
|
jsval_release(res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-08-29 00:02:49 +02:00
|
|
|
|
|
|
|
if(this_obj)
|
|
|
|
IDispatch_Release(this_obj);
|
2012-06-25 14:08:38 +02:00
|
|
|
for(i=0; i < cnt; i++)
|
2012-09-17 15:16:42 +02:00
|
|
|
jsval_release(args[i]);
|
2012-06-25 14:08:38 +02:00
|
|
|
heap_free(args);
|
2009-08-29 00:02:49 +02:00
|
|
|
return hres;
|
2008-09-09 01:27:01 +02:00
|
|
|
}
|
|
|
|
|
2012-09-17 15:16:42 +02:00
|
|
|
static HRESULT Function_call(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
|
2012-09-18 19:01:49 +02:00
|
|
|
jsval_t *r)
|
2008-09-09 01:27:01 +02:00
|
|
|
{
|
2009-08-29 00:02:22 +02:00
|
|
|
FunctionInstance *function;
|
|
|
|
IDispatch *this_obj = NULL;
|
2012-06-25 14:08:38 +02:00
|
|
|
unsigned cnt = 0;
|
2009-08-29 00:02:22 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
2009-09-24 00:45:42 +02:00
|
|
|
if(!(function = function_this(jsthis)))
|
2012-09-18 19:01:49 +02:00
|
|
|
return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
|
2009-08-29 00:02:22 +02:00
|
|
|
|
|
|
|
if(argc) {
|
2012-09-17 15:16:42 +02:00
|
|
|
if(!is_undefined(argv[0]) && !is_null(argv[0])) {
|
2012-09-17 15:19:19 +02:00
|
|
|
hres = to_object(ctx, argv[0], &this_obj);
|
2009-10-30 00:01:49 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
2012-06-25 14:08:38 +02:00
|
|
|
cnt = argc-1;
|
2009-08-29 00:02:22 +02:00
|
|
|
}
|
|
|
|
|
2019-08-14 13:18:09 +02:00
|
|
|
hres = function->vtbl->call(ctx, function, this_obj, flags, cnt, argv + 1, r);
|
2009-08-29 00:02:22 +02:00
|
|
|
|
|
|
|
if(this_obj)
|
|
|
|
IDispatch_Release(this_obj);
|
|
|
|
return hres;
|
2008-09-09 01:27:01 +02:00
|
|
|
}
|
|
|
|
|
2019-08-14 13:18:52 +02:00
|
|
|
static HRESULT Function_bind(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
|
|
|
|
jsval_t *r)
|
|
|
|
{
|
|
|
|
FunctionInstance *function;
|
|
|
|
jsdisp_t *new_function;
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
|
|
|
if(!(function = function_this(jsthis)))
|
|
|
|
return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
|
|
|
|
|
|
|
|
if(argc < 1) {
|
|
|
|
FIXME("no this argument\n");
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!is_object_instance(argv[0]) || !get_object(argv[0])) {
|
|
|
|
FIXME("%s is not an object instance\n", debugstr_jsval(argv[0]));
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
hres = create_bind_function(ctx, function, get_object(argv[0]), argc - 1, argv + 1, &new_function);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
if(r)
|
|
|
|
*r = jsval_obj(new_function);
|
|
|
|
else
|
|
|
|
jsdisp_release(new_function);
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-17 15:16:42 +02:00
|
|
|
HRESULT Function_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
|
2012-09-18 19:01:49 +02:00
|
|
|
jsval_t *r)
|
2008-09-09 01:27:01 +02:00
|
|
|
{
|
2008-09-10 21:06:09 +02:00
|
|
|
FunctionInstance *function;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
2009-09-23 16:18:39 +02:00
|
|
|
if(!is_vclass(jsthis, JSCLASS_FUNCTION)) {
|
2008-09-10 21:06:09 +02:00
|
|
|
ERR("dispex is not a function\n");
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
2016-09-22 09:56:28 +02:00
|
|
|
function = function_from_jsdisp(jsthis->u.jsdisp);
|
2019-08-14 13:18:09 +02:00
|
|
|
return function->vtbl->call(ctx, function, NULL, flags, argc, argv, r);
|
2015-02-05 14:49:01 +01:00
|
|
|
}
|
2008-09-21 15:48:11 +02:00
|
|
|
|
2015-02-06 12:09:40 +01:00
|
|
|
HRESULT Function_get_value(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
|
2015-02-05 14:49:01 +01:00
|
|
|
{
|
2019-08-14 13:17:51 +02:00
|
|
|
FunctionInstance *function = function_from_jsdisp(jsthis);
|
2015-02-05 14:49:01 +01:00
|
|
|
jsstr_t *str;
|
|
|
|
HRESULT hres;
|
2008-09-21 15:48:11 +02:00
|
|
|
|
2015-02-05 14:49:01 +01:00
|
|
|
TRACE("\n");
|
2008-09-10 21:08:26 +02:00
|
|
|
|
2019-08-14 13:17:51 +02:00
|
|
|
hres = function->vtbl->toString(function, &str);
|
2015-02-05 14:49:01 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
2008-09-10 21:06:09 +02:00
|
|
|
|
2015-02-05 14:49:01 +01:00
|
|
|
*r = jsval_string(str);
|
2008-09-10 21:06:09 +02:00
|
|
|
return S_OK;
|
2008-09-09 01:27:01 +02:00
|
|
|
}
|
|
|
|
|
2015-02-06 12:09:40 +01:00
|
|
|
static HRESULT Function_get_arguments(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
|
2010-07-27 11:31:01 +02:00
|
|
|
{
|
2015-02-06 12:09:40 +01:00
|
|
|
FunctionInstance *function = function_from_jsdisp(jsthis);
|
2016-03-28 17:51:28 +02:00
|
|
|
call_frame_t *frame;
|
2016-08-03 16:28:26 +02:00
|
|
|
HRESULT hres;
|
2010-07-27 11:31:01 +02:00
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
2016-03-28 17:51:28 +02:00
|
|
|
for(frame = ctx->call_ctx; frame; frame = frame->prev_frame) {
|
|
|
|
if(frame->function_instance == &function->dispex) {
|
2016-08-03 16:28:26 +02:00
|
|
|
if(!frame->arguments_obj) {
|
|
|
|
hres = setup_arguments_object(ctx, frame);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
}
|
2016-03-28 17:51:28 +02:00
|
|
|
*r = jsval_obj(jsdisp_addref(frame->arguments_obj));
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*r = jsval_null();
|
2015-02-05 14:49:01 +01:00
|
|
|
return S_OK;
|
2010-07-27 11:31:01 +02:00
|
|
|
}
|
|
|
|
|
2019-12-12 13:53:55 +01:00
|
|
|
function_code_t *Function_get_code(jsdisp_t *jsthis)
|
|
|
|
{
|
|
|
|
FunctionInstance *function;
|
|
|
|
|
|
|
|
assert(is_class(jsthis, JSCLASS_FUNCTION));
|
|
|
|
function = function_from_jsdisp(jsthis);
|
|
|
|
|
|
|
|
return function->vtbl->get_code(function);
|
|
|
|
}
|
|
|
|
|
2010-09-06 16:11:49 +02:00
|
|
|
static void Function_destructor(jsdisp_t *dispex)
|
2008-09-09 01:27:01 +02:00
|
|
|
{
|
2019-08-14 13:18:00 +02:00
|
|
|
FunctionInstance *function = function_from_jsdisp(dispex);
|
|
|
|
function->vtbl->destructor(function);
|
|
|
|
heap_free(function);
|
2008-09-09 01:27:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const builtin_prop_t Function_props[] = {
|
2009-08-23 23:38:16 +02:00
|
|
|
{applyW, Function_apply, PROPF_METHOD|2},
|
2018-05-10 19:20:36 +02:00
|
|
|
{argumentsW, NULL, 0, Function_get_arguments},
|
2019-08-14 13:18:52 +02:00
|
|
|
{bindW, Function_bind, PROPF_METHOD|PROPF_ES5|1},
|
2009-08-23 23:38:16 +02:00
|
|
|
{callW, Function_call, PROPF_METHOD|1},
|
2018-05-10 19:20:36 +02:00
|
|
|
{lengthW, NULL, 0, Function_get_length},
|
2009-07-12 19:52:31 +02:00
|
|
|
{toStringW, Function_toString, PROPF_METHOD}
|
2008-09-09 01:27:01 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static const builtin_info_t Function_info = {
|
|
|
|
JSCLASS_FUNCTION,
|
2015-02-05 14:49:01 +01:00
|
|
|
DEFAULT_FUNCTION_VALUE,
|
2018-07-09 20:59:12 +02:00
|
|
|
ARRAY_SIZE(Function_props),
|
2008-09-09 01:27:01 +02:00
|
|
|
Function_props,
|
|
|
|
Function_destructor,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2012-07-23 15:53:38 +02:00
|
|
|
static const builtin_prop_t FunctionInst_props[] = {
|
2018-05-10 19:20:36 +02:00
|
|
|
{argumentsW, NULL, 0, Function_get_arguments},
|
|
|
|
{lengthW, NULL, 0, Function_get_length}
|
2012-07-23 15:53:38 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static const builtin_info_t FunctionInst_info = {
|
|
|
|
JSCLASS_FUNCTION,
|
2015-02-05 14:49:01 +01:00
|
|
|
DEFAULT_FUNCTION_VALUE,
|
2018-07-09 20:59:12 +02:00
|
|
|
ARRAY_SIZE(FunctionInst_props),
|
2012-07-23 15:53:38 +02:00
|
|
|
FunctionInst_props,
|
|
|
|
Function_destructor,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2019-08-14 13:17:51 +02:00
|
|
|
static HRESULT create_function(script_ctx_t *ctx, const builtin_info_t *builtin_info, const function_vtbl_t *vtbl, size_t size,
|
|
|
|
DWORD flags, BOOL funcprot, jsdisp_t *prototype, void **ret)
|
2008-09-09 01:27:01 +02:00
|
|
|
{
|
|
|
|
FunctionInstance *function;
|
|
|
|
HRESULT hres;
|
|
|
|
|
2019-08-14 13:17:51 +02:00
|
|
|
function = heap_alloc_zero(size);
|
2008-09-09 01:27:01 +02:00
|
|
|
if(!function)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2008-09-19 00:46:26 +02:00
|
|
|
if(funcprot)
|
2012-07-23 15:53:38 +02:00
|
|
|
hres = init_dispex(&function->dispex, ctx, builtin_info, prototype);
|
2009-07-03 02:45:12 +02:00
|
|
|
else if(builtin_info)
|
|
|
|
hres = init_dispex_from_constr(&function->dispex, ctx, builtin_info, ctx->function_constr);
|
2008-09-19 00:46:26 +02:00
|
|
|
else
|
2012-07-23 15:53:38 +02:00
|
|
|
hres = init_dispex_from_constr(&function->dispex, ctx, &FunctionInst_info, ctx->function_constr);
|
2012-10-31 10:21:09 +01:00
|
|
|
if(FAILED(hres)) {
|
|
|
|
heap_free(function);
|
2008-09-09 01:27:01 +02:00
|
|
|
return hres;
|
2012-10-31 10:21:09 +01:00
|
|
|
}
|
2008-09-09 01:27:01 +02:00
|
|
|
|
2019-08-14 13:17:51 +02:00
|
|
|
function->vtbl = vtbl;
|
2008-09-09 01:27:01 +02:00
|
|
|
function->flags = flags;
|
|
|
|
function->length = flags & PROPF_ARGMASK;
|
|
|
|
|
2009-07-12 19:52:31 +02:00
|
|
|
*ret = function;
|
|
|
|
return S_OK;
|
|
|
|
}
|
2008-09-10 02:33:15 +02:00
|
|
|
|
2019-08-14 13:18:21 +02:00
|
|
|
static HRESULT NativeFunction_call(script_ctx_t *ctx, FunctionInstance *func, IDispatch *this_disp, unsigned flags,
|
2019-08-14 13:18:09 +02:00
|
|
|
unsigned argc, jsval_t *argv, jsval_t *r)
|
|
|
|
{
|
2019-08-14 13:18:21 +02:00
|
|
|
NativeFunction *function = (NativeFunction*)func;
|
2019-08-14 13:18:09 +02:00
|
|
|
vdisp_t vthis;
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
if(this_disp)
|
|
|
|
set_disp(&vthis, this_disp);
|
|
|
|
else if(ctx->host_global)
|
|
|
|
set_disp(&vthis, ctx->host_global);
|
|
|
|
else
|
|
|
|
set_jsdisp(&vthis, ctx->global);
|
|
|
|
|
2019-08-14 13:18:21 +02:00
|
|
|
hres = function->proc(ctx, &vthis, flags & ~DISPATCH_JSCRIPT_INTERNAL_MASK, argc, argv, r);
|
2019-08-14 13:18:09 +02:00
|
|
|
|
|
|
|
vdisp_release(&vthis);
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
2019-08-14 13:18:39 +02:00
|
|
|
static HRESULT NativeFunction_toString(FunctionInstance *func, jsstr_t **ret)
|
2019-08-14 13:17:51 +02:00
|
|
|
{
|
2019-08-14 13:18:39 +02:00
|
|
|
NativeFunction *function = (NativeFunction*)func;
|
2019-08-14 13:17:51 +02:00
|
|
|
DWORD name_len;
|
|
|
|
jsstr_t *str;
|
|
|
|
WCHAR *ptr;
|
|
|
|
|
|
|
|
static const WCHAR native_prefixW[] = {'\n','f','u','n','c','t','i','o','n',' '};
|
|
|
|
static const WCHAR native_suffixW[] =
|
|
|
|
{'(',')',' ','{','\n',' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n','}','\n'};
|
|
|
|
|
|
|
|
name_len = function->name ? lstrlenW(function->name) : 0;
|
|
|
|
str = jsstr_alloc_buf(ARRAY_SIZE(native_prefixW) + ARRAY_SIZE(native_suffixW) + name_len, &ptr);
|
|
|
|
if(!str)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
memcpy(ptr, native_prefixW, sizeof(native_prefixW));
|
|
|
|
ptr += ARRAY_SIZE(native_prefixW);
|
|
|
|
memcpy(ptr, function->name, name_len*sizeof(WCHAR));
|
|
|
|
ptr += name_len;
|
|
|
|
memcpy(ptr, native_suffixW, sizeof(native_suffixW));
|
|
|
|
|
|
|
|
*ret = str;
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2019-12-12 13:53:55 +01:00
|
|
|
static function_code_t *NativeFunction_get_code(FunctionInstance *function)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-08-14 13:18:00 +02:00
|
|
|
static void NativeFunction_destructor(FunctionInstance *function)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-08-14 13:17:51 +02:00
|
|
|
static const function_vtbl_t NativeFunctionVtbl = {
|
2019-08-14 13:18:09 +02:00
|
|
|
NativeFunction_call,
|
2019-08-14 13:17:51 +02:00
|
|
|
NativeFunction_toString,
|
2019-12-12 13:53:55 +01:00
|
|
|
NativeFunction_get_code,
|
2019-08-14 13:18:00 +02:00
|
|
|
NativeFunction_destructor
|
2019-08-14 13:17:51 +02:00
|
|
|
};
|
|
|
|
|
2009-09-17 01:04:44 +02:00
|
|
|
HRESULT create_builtin_function(script_ctx_t *ctx, builtin_invoke_t value_proc, const WCHAR *name,
|
2010-09-06 16:11:49 +02:00
|
|
|
const builtin_info_t *builtin_info, DWORD flags, jsdisp_t *prototype, jsdisp_t **ret)
|
2008-09-09 01:27:01 +02:00
|
|
|
{
|
2019-08-14 13:18:21 +02:00
|
|
|
NativeFunction *function;
|
2008-09-09 01:27:01 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
2019-08-14 13:17:51 +02:00
|
|
|
hres = create_function(ctx, builtin_info, &NativeFunctionVtbl, sizeof(NativeFunction), flags, FALSE, NULL, (void**)&function);
|
2008-09-09 01:27:01 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-09-17 15:17:24 +02:00
|
|
|
if(builtin_info)
|
2019-08-14 13:18:21 +02:00
|
|
|
hres = jsdisp_define_data_property(&function->function.dispex, lengthW, 0,
|
|
|
|
jsval_number(function->function.length));
|
2010-05-26 19:17:21 +02:00
|
|
|
if(SUCCEEDED(hres))
|
2019-08-14 13:18:21 +02:00
|
|
|
hres = jsdisp_define_data_property(&function->function.dispex, prototypeW, 0, jsval_obj(prototype));
|
2009-07-12 19:52:31 +02:00
|
|
|
if(FAILED(hres)) {
|
2019-08-14 13:18:21 +02:00
|
|
|
jsdisp_release(&function->function.dispex);
|
2009-07-12 19:52:31 +02:00
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
2019-08-14 13:18:21 +02:00
|
|
|
function->proc = value_proc;
|
2019-08-14 13:18:39 +02:00
|
|
|
function->name = name;
|
2008-09-09 01:27:01 +02:00
|
|
|
|
2019-08-14 13:18:21 +02:00
|
|
|
*ret = &function->function.dispex;
|
2008-09-09 01:27:01 +02:00
|
|
|
return S_OK;
|
|
|
|
}
|
2008-09-09 01:27:57 +02:00
|
|
|
|
2012-07-16 15:31:24 +02:00
|
|
|
static HRESULT set_constructor_prop(script_ctx_t *ctx, jsdisp_t *constr, jsdisp_t *prot)
|
|
|
|
{
|
|
|
|
static const WCHAR constructorW[] = {'c','o','n','s','t','r','u','c','t','o','r',0};
|
|
|
|
|
2018-05-14 13:34:02 +02:00
|
|
|
return jsdisp_define_data_property(prot, constructorW, PROPF_WRITABLE | PROPF_CONFIGURABLE,
|
|
|
|
jsval_obj(constr));
|
2012-07-16 15:31:24 +02:00
|
|
|
}
|
|
|
|
|
2012-07-16 15:30:54 +02:00
|
|
|
HRESULT create_builtin_constructor(script_ctx_t *ctx, builtin_invoke_t value_proc, const WCHAR *name,
|
|
|
|
const builtin_info_t *builtin_info, DWORD flags, jsdisp_t *prototype, jsdisp_t **ret)
|
|
|
|
{
|
|
|
|
jsdisp_t *constr;
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
hres = create_builtin_function(ctx, value_proc, name, builtin_info, flags, prototype, &constr);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-07-16 15:31:24 +02:00
|
|
|
hres = set_constructor_prop(ctx, constr, prototype);
|
2012-07-16 15:30:54 +02:00
|
|
|
if(FAILED(hres)) {
|
|
|
|
jsdisp_release(constr);
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ret = constr;
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2019-08-14 13:18:30 +02:00
|
|
|
static HRESULT InterpretedFunction_call(script_ctx_t *ctx, FunctionInstance *func, IDispatch *this_obj, unsigned flags,
|
2019-08-14 13:18:09 +02:00
|
|
|
unsigned argc, jsval_t *argv, jsval_t *r)
|
|
|
|
{
|
2019-08-14 13:18:30 +02:00
|
|
|
InterpretedFunction *function = (InterpretedFunction*)func;
|
2019-08-14 13:18:09 +02:00
|
|
|
jsdisp_t *var_disp, *new_obj = NULL;
|
|
|
|
DWORD exec_flags = 0;
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
TRACE("%p\n", function);
|
|
|
|
|
|
|
|
if(ctx->state == SCRIPTSTATE_UNINITIALIZED || ctx->state == SCRIPTSTATE_CLOSED) {
|
|
|
|
WARN("Script engine state does not allow running code.\n");
|
|
|
|
return E_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(flags & DISPATCH_CONSTRUCT) {
|
2019-08-14 13:18:30 +02:00
|
|
|
hres = create_object(ctx, &function->function.dispex, &new_obj);
|
2019-08-14 13:18:09 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
this_obj = to_disp(new_obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(flags & DISPATCH_JSCRIPT_CALLEREXECSSOURCE)
|
|
|
|
exec_flags |= EXEC_RETURN_TO_INTERP;
|
|
|
|
if(flags & DISPATCH_CONSTRUCT)
|
|
|
|
exec_flags |= EXEC_CONSTRUCTOR;
|
|
|
|
|
|
|
|
hres = create_dispex(ctx, NULL, NULL, &var_disp);
|
|
|
|
if(SUCCEEDED(hres))
|
|
|
|
hres = exec_source(ctx, exec_flags, function->code, function->func_code, function->scope_chain, this_obj,
|
2019-08-14 13:18:30 +02:00
|
|
|
&function->function.dispex, var_disp, argc, argv, r);
|
2019-08-14 13:18:09 +02:00
|
|
|
if(new_obj)
|
|
|
|
jsdisp_release(new_obj);
|
|
|
|
|
|
|
|
jsdisp_release(var_disp);
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
2019-08-14 13:18:30 +02:00
|
|
|
static HRESULT InterpretedFunction_toString(FunctionInstance *func, jsstr_t **ret)
|
2019-08-14 13:17:51 +02:00
|
|
|
{
|
2019-08-14 13:18:30 +02:00
|
|
|
InterpretedFunction *function = (InterpretedFunction*)func;
|
|
|
|
|
2019-08-14 13:17:51 +02:00
|
|
|
*ret = jsstr_alloc_len(function->func_code->source, function->func_code->source_len);
|
|
|
|
return *ret ? S_OK : E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
|
2019-12-12 13:53:55 +01:00
|
|
|
static function_code_t *InterpretedFunction_get_code(FunctionInstance *func)
|
|
|
|
{
|
|
|
|
InterpretedFunction *function = (InterpretedFunction*)func;
|
|
|
|
|
|
|
|
return function->func_code;
|
|
|
|
}
|
|
|
|
|
2019-08-14 13:18:30 +02:00
|
|
|
static void InterpretedFunction_destructor(FunctionInstance *func)
|
2019-08-14 13:18:00 +02:00
|
|
|
{
|
2019-08-14 13:18:30 +02:00
|
|
|
InterpretedFunction *function = (InterpretedFunction*)func;
|
|
|
|
|
2019-08-14 13:18:00 +02:00
|
|
|
release_bytecode(function->code);
|
|
|
|
if(function->scope_chain)
|
|
|
|
scope_release(function->scope_chain);
|
|
|
|
}
|
|
|
|
|
2019-08-14 13:17:51 +02:00
|
|
|
static const function_vtbl_t InterpretedFunctionVtbl = {
|
2019-08-14 13:18:09 +02:00
|
|
|
InterpretedFunction_call,
|
2019-08-14 13:17:51 +02:00
|
|
|
InterpretedFunction_toString,
|
2019-12-12 13:53:55 +01:00
|
|
|
InterpretedFunction_get_code,
|
2019-08-14 13:18:00 +02:00
|
|
|
InterpretedFunction_destructor
|
2019-08-14 13:17:51 +02:00
|
|
|
};
|
|
|
|
|
2012-04-25 11:25:57 +02:00
|
|
|
HRESULT create_source_function(script_ctx_t *ctx, bytecode_t *code, function_code_t *func_code,
|
2012-04-24 17:39:40 +02:00
|
|
|
scope_chain_t *scope_chain, jsdisp_t **ret)
|
2008-09-09 01:27:57 +02:00
|
|
|
{
|
2019-08-14 13:18:30 +02:00
|
|
|
InterpretedFunction *function;
|
2010-09-06 16:11:49 +02:00
|
|
|
jsdisp_t *prototype;
|
2008-09-09 01:27:57 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
2012-03-12 19:24:22 +01:00
|
|
|
hres = create_object(ctx, NULL, &prototype);
|
2008-09-10 21:09:04 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2019-08-14 13:18:30 +02:00
|
|
|
hres = create_function(ctx, NULL, &InterpretedFunctionVtbl, sizeof(InterpretedFunction), PROPF_CONSTR,
|
2019-08-14 13:17:51 +02:00
|
|
|
FALSE, NULL, (void**)&function);
|
2009-07-12 19:52:31 +02:00
|
|
|
if(SUCCEEDED(hres)) {
|
2019-08-14 13:18:30 +02:00
|
|
|
hres = jsdisp_define_data_property(&function->function.dispex, prototypeW, PROPF_WRITABLE,
|
2018-05-14 13:33:52 +02:00
|
|
|
jsval_obj(prototype));
|
2012-07-16 15:32:15 +02:00
|
|
|
if(SUCCEEDED(hres))
|
2019-08-14 13:18:30 +02:00
|
|
|
hres = set_constructor_prop(ctx, &function->function.dispex, prototype);
|
2009-07-12 19:52:31 +02:00
|
|
|
if(FAILED(hres))
|
2019-08-14 13:18:30 +02:00
|
|
|
jsdisp_release(&function->function.dispex);
|
2009-07-12 19:52:31 +02:00
|
|
|
}
|
2008-09-10 21:09:04 +02:00
|
|
|
jsdisp_release(prototype);
|
2008-09-09 01:27:57 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
if(scope_chain) {
|
|
|
|
scope_addref(scope_chain);
|
|
|
|
function->scope_chain = scope_chain;
|
|
|
|
}
|
|
|
|
|
2012-03-12 19:24:22 +01:00
|
|
|
bytecode_addref(code);
|
|
|
|
function->code = code;
|
2012-04-25 11:25:57 +02:00
|
|
|
function->func_code = func_code;
|
2019-08-14 13:18:30 +02:00
|
|
|
function->function.length = function->func_code->param_cnt;
|
2008-09-09 01:27:57 +02:00
|
|
|
|
2019-08-14 13:18:30 +02:00
|
|
|
*ret = &function->function.dispex;
|
2008-09-09 01:27:57 +02:00
|
|
|
return S_OK;
|
|
|
|
}
|
2008-09-19 00:46:26 +02:00
|
|
|
|
2019-08-14 13:18:52 +02:00
|
|
|
static HRESULT BindFunction_call(script_ctx_t *ctx, FunctionInstance *func, IDispatch *this_obj, unsigned flags,
|
|
|
|
unsigned argc, jsval_t *argv, jsval_t *r)
|
|
|
|
{
|
|
|
|
BindFunction *function = (BindFunction*)func;
|
|
|
|
jsval_t *call_args = NULL;
|
|
|
|
unsigned call_argc;
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
TRACE("%p\n", function);
|
|
|
|
|
|
|
|
call_argc = function->argc + argc;
|
|
|
|
if(call_argc) {
|
|
|
|
call_args = heap_alloc(function->argc * sizeof(*function->args));
|
|
|
|
if(!call_args)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
if(function->argc)
|
|
|
|
memcpy(call_args, function->args, function->argc * sizeof(*call_args));
|
|
|
|
if(argc)
|
|
|
|
memcpy(call_args + function->argc, argv, argc * sizeof(*call_args));
|
|
|
|
}
|
|
|
|
|
|
|
|
hres = function->target->vtbl->call(ctx, function->target, function->this, flags, call_argc, call_args, r);
|
|
|
|
|
|
|
|
heap_free(call_args);
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT BindFunction_toString(FunctionInstance *function, jsstr_t **ret)
|
|
|
|
{
|
|
|
|
static const WCHAR native_functionW[] =
|
|
|
|
{'\n','f','u','n','c','t','i','o','n','(',')',' ','{','\n',
|
|
|
|
' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n',
|
|
|
|
'}','\n',0};
|
|
|
|
|
|
|
|
*ret = jsstr_alloc(native_functionW);
|
|
|
|
return *ret ? S_OK : E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
|
2019-12-12 13:53:55 +01:00
|
|
|
static function_code_t *BindFunction_get_code(FunctionInstance *function)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-08-14 13:18:52 +02:00
|
|
|
static void BindFunction_destructor(FunctionInstance *func)
|
|
|
|
{
|
|
|
|
BindFunction *function = (BindFunction*)func;
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
TRACE("%p\n", function);
|
|
|
|
|
|
|
|
for(i = 0; i < function->argc; i++)
|
|
|
|
jsval_release(function->args[i]);
|
|
|
|
jsdisp_release(&function->target->dispex);
|
|
|
|
IDispatch_Release(function->this);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const function_vtbl_t BindFunctionVtbl = {
|
|
|
|
BindFunction_call,
|
|
|
|
BindFunction_toString,
|
2019-12-12 13:53:55 +01:00
|
|
|
BindFunction_get_code,
|
2019-08-14 13:18:52 +02:00
|
|
|
BindFunction_destructor
|
|
|
|
};
|
|
|
|
|
|
|
|
static HRESULT create_bind_function(script_ctx_t *ctx, FunctionInstance *target, IDispatch *bound_this, unsigned argc,
|
|
|
|
jsval_t *argv, jsdisp_t **ret)
|
|
|
|
{
|
|
|
|
BindFunction *function;
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
hres = create_function(ctx, NULL, &BindFunctionVtbl, FIELD_OFFSET(BindFunction, args[argc]), PROPF_METHOD,
|
|
|
|
FALSE, NULL, (void**)&function);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
jsdisp_addref(&target->dispex);
|
|
|
|
function->target = target;
|
|
|
|
|
|
|
|
IDispatch_AddRef(function->this = bound_this);
|
|
|
|
|
|
|
|
for(function->argc = 0; function->argc < argc; function->argc++) {
|
|
|
|
hres = jsval_copy(argv[function->argc], function->args + function->argc);
|
|
|
|
if(FAILED(hres)) {
|
|
|
|
jsdisp_release(&function->function.dispex);
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function->function.length = target->length > argc ? target->length - argc : 0;
|
|
|
|
|
|
|
|
*ret = &function->function.dispex;
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-18 19:01:49 +02:00
|
|
|
static HRESULT construct_function(script_ctx_t *ctx, unsigned argc, jsval_t *argv, IDispatch **ret)
|
2009-12-10 01:14:58 +01:00
|
|
|
{
|
|
|
|
WCHAR *str = NULL, *ptr;
|
2013-03-05 11:49:21 +01:00
|
|
|
unsigned len = 0, i = 0;
|
2012-03-12 19:24:22 +01:00
|
|
|
bytecode_t *code;
|
2010-09-06 16:11:49 +02:00
|
|
|
jsdisp_t *function;
|
2012-10-11 12:16:01 +02:00
|
|
|
jsstr_t **params = NULL;
|
2013-02-09 23:32:02 +01:00
|
|
|
int j = 0;
|
2009-12-10 01:14:58 +01:00
|
|
|
HRESULT hres = S_OK;
|
|
|
|
|
|
|
|
static const WCHAR function_anonymousW[] = {'f','u','n','c','t','i','o','n',' ','a','n','o','n','y','m','o','u','s','('};
|
|
|
|
static const WCHAR function_beginW[] = {')',' ','{','\n'};
|
|
|
|
static const WCHAR function_endW[] = {'\n','}',0};
|
|
|
|
|
|
|
|
if(argc) {
|
2012-10-11 12:16:01 +02:00
|
|
|
params = heap_alloc(argc*sizeof(*params));
|
2009-12-10 01:14:58 +01:00
|
|
|
if(!params)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
if(argc > 2)
|
|
|
|
len = (argc-2)*2; /* separating commas */
|
|
|
|
for(i=0; i < argc; i++) {
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_string(ctx, argv[i], params+i);
|
2009-12-10 01:14:58 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
break;
|
2012-10-11 12:16:01 +02:00
|
|
|
len += jsstr_length(params[i]);
|
2009-12-10 01:14:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(SUCCEEDED(hres)) {
|
2018-07-09 20:59:12 +02:00
|
|
|
len += ARRAY_SIZE(function_anonymousW) + ARRAY_SIZE(function_beginW) + ARRAY_SIZE(function_endW);
|
2009-12-10 01:14:58 +01:00
|
|
|
str = heap_alloc(len*sizeof(WCHAR));
|
|
|
|
if(str) {
|
|
|
|
memcpy(str, function_anonymousW, sizeof(function_anonymousW));
|
2018-07-09 20:59:12 +02:00
|
|
|
ptr = str + ARRAY_SIZE(function_anonymousW);
|
2009-12-10 01:14:58 +01:00
|
|
|
if(argc > 1) {
|
|
|
|
while(1) {
|
2013-03-05 11:49:21 +01:00
|
|
|
ptr += jsstr_flush(params[j], ptr);
|
2009-12-10 01:14:58 +01:00
|
|
|
if(++j == argc-1)
|
|
|
|
break;
|
|
|
|
*ptr++ = ',';
|
|
|
|
*ptr++ = ' ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
memcpy(ptr, function_beginW, sizeof(function_beginW));
|
2018-07-09 20:59:12 +02:00
|
|
|
ptr += ARRAY_SIZE(function_beginW);
|
2013-03-05 11:49:21 +01:00
|
|
|
if(argc)
|
|
|
|
ptr += jsstr_flush(params[argc-1], ptr);
|
2009-12-10 01:14:58 +01:00
|
|
|
memcpy(ptr, function_endW, sizeof(function_endW));
|
|
|
|
|
|
|
|
TRACE("%s\n", debugstr_w(str));
|
|
|
|
}else {
|
|
|
|
hres = E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-09 23:32:02 +01:00
|
|
|
while(i)
|
|
|
|
jsstr_release(params[--i]);
|
2009-12-10 01:14:58 +01:00
|
|
|
heap_free(params);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-10-04 14:58:05 +02:00
|
|
|
hres = compile_script(ctx, str, NULL, NULL, FALSE, FALSE, &code);
|
2009-12-10 01:14:58 +01:00
|
|
|
heap_free(str);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2016-08-04 13:39:46 +02:00
|
|
|
if(code->global_code.func_cnt != 1 || code->global_code.var_cnt != 1) {
|
2009-12-10 01:14:58 +01:00
|
|
|
ERR("Invalid parser result!\n");
|
2012-03-12 19:24:22 +01:00
|
|
|
release_bytecode(code);
|
2009-12-10 01:14:58 +01:00
|
|
|
return E_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2012-04-25 11:25:57 +02:00
|
|
|
hres = create_source_function(ctx, code, code->global_code.funcs, NULL, &function);
|
2012-03-12 19:24:22 +01:00
|
|
|
release_bytecode(code);
|
2009-12-10 01:14:58 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2010-09-07 15:42:01 +02:00
|
|
|
*ret = to_disp(function);
|
2009-12-10 01:14:58 +01:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-17 15:16:42 +02:00
|
|
|
static HRESULT FunctionConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
|
2012-09-18 19:01:49 +02:00
|
|
|
jsval_t *r)
|
2009-12-10 01:14:27 +01:00
|
|
|
{
|
2009-12-10 01:14:58 +01:00
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
|
|
|
switch(flags) {
|
2016-01-06 16:29:42 +01:00
|
|
|
case DISPATCH_METHOD:
|
2009-12-10 01:14:58 +01:00
|
|
|
case DISPATCH_CONSTRUCT: {
|
|
|
|
IDispatch *ret;
|
|
|
|
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = construct_function(ctx, argc, argv, &ret);
|
2009-12-10 01:14:58 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
*r = jsval_disp(ret);
|
2009-12-10 01:14:58 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
FIXME("unimplemented flags %x\n", flags);
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
2009-12-10 01:14:27 +01:00
|
|
|
}
|
|
|
|
|
2012-09-17 15:16:42 +02:00
|
|
|
static HRESULT FunctionProt_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
|
2012-09-18 19:01:49 +02:00
|
|
|
jsval_t *r)
|
2009-12-10 01:14:27 +01:00
|
|
|
{
|
|
|
|
FIXME("\n");
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
2010-09-06 16:11:49 +02:00
|
|
|
HRESULT init_function_constr(script_ctx_t *ctx, jsdisp_t *object_prototype)
|
2008-09-19 00:46:26 +02:00
|
|
|
{
|
2019-08-14 13:17:51 +02:00
|
|
|
NativeFunction *prot, *constr;
|
2008-09-19 00:46:26 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
2009-09-17 01:04:44 +02:00
|
|
|
static const WCHAR FunctionW[] = {'F','u','n','c','t','i','o','n',0};
|
|
|
|
|
2019-08-14 13:17:51 +02:00
|
|
|
hres = create_function(ctx, &Function_info, &NativeFunctionVtbl, sizeof(NativeFunction), PROPF_CONSTR,
|
|
|
|
TRUE, object_prototype, (void**)&prot);
|
2008-09-19 00:46:26 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2019-08-14 13:18:21 +02:00
|
|
|
prot->proc = FunctionProt_value;
|
2019-08-14 13:18:39 +02:00
|
|
|
prot->name = prototypeW;
|
2008-09-19 00:46:26 +02:00
|
|
|
|
2019-08-14 13:17:51 +02:00
|
|
|
hres = create_function(ctx, &FunctionInst_info, &NativeFunctionVtbl, sizeof(NativeFunction), PROPF_CONSTR|1,
|
|
|
|
TRUE, &prot->function.dispex, (void**)&constr);
|
2009-07-12 19:52:31 +02:00
|
|
|
if(SUCCEEDED(hres)) {
|
2019-08-14 13:18:21 +02:00
|
|
|
constr->proc = FunctionConstr_value;
|
2019-08-14 13:18:39 +02:00
|
|
|
constr->name = FunctionW;
|
2019-08-14 13:17:51 +02:00
|
|
|
hres = jsdisp_define_data_property(&constr->function.dispex, prototypeW, 0, jsval_obj(&prot->function.dispex));
|
2012-07-16 15:31:24 +02:00
|
|
|
if(SUCCEEDED(hres))
|
2019-08-14 13:17:51 +02:00
|
|
|
hres = set_constructor_prop(ctx, &constr->function.dispex, &prot->function.dispex);
|
2009-07-12 19:52:31 +02:00
|
|
|
if(FAILED(hres))
|
2019-08-14 13:17:51 +02:00
|
|
|
jsdisp_release(&constr->function.dispex);
|
2009-07-12 19:52:31 +02:00
|
|
|
}
|
2019-08-14 13:17:51 +02:00
|
|
|
jsdisp_release(&prot->function.dispex);
|
2008-09-19 00:46:26 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2019-08-14 13:17:51 +02:00
|
|
|
ctx->function_constr = &constr->function.dispex;
|
2009-07-12 19:52:31 +02:00
|
|
|
return S_OK;
|
2008-09-19 00:46:26 +02:00
|
|
|
}
|