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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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);
|
|
|
|
|
|
|
|
typedef struct {
|
2010-09-06 16:11:49 +02:00
|
|
|
jsdisp_t dispex;
|
2008-09-09 01:27:01 +02:00
|
|
|
builtin_invoke_t value_proc;
|
2009-09-17 01:04:44 +02:00
|
|
|
const WCHAR *name;
|
2008-09-09 01:27:01 +02:00
|
|
|
DWORD flags;
|
2008-09-09 01:27:57 +02:00
|
|
|
source_elements_t *source;
|
|
|
|
parameter_t *parameters;
|
|
|
|
scope_chain_t *scope_chain;
|
|
|
|
parser_ctx_t *parser;
|
2008-09-21 15:47:47 +02:00
|
|
|
const WCHAR *src_str;
|
|
|
|
DWORD src_len;
|
2008-09-09 01:27:01 +02:00
|
|
|
DWORD length;
|
2010-09-06 16:11:49 +02:00
|
|
|
jsdisp_t *arguments;
|
2008-09-09 01:27:01 +02:00
|
|
|
} FunctionInstance;
|
|
|
|
|
2009-09-23 16:18:39 +02:00
|
|
|
static inline FunctionInstance *function_from_vdisp(vdisp_t *vdisp)
|
|
|
|
{
|
|
|
|
return (FunctionInstance*)vdisp->u.jsdisp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline FunctionInstance *function_this(vdisp_t *jsthis)
|
|
|
|
{
|
|
|
|
return is_vclass(jsthis, JSCLASS_FUNCTION) ? function_from_vdisp(jsthis) : NULL;
|
|
|
|
}
|
|
|
|
|
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};
|
|
|
|
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
|
|
|
|
2008-09-10 21:06:09 +02:00
|
|
|
static IDispatch *get_this(DISPPARAMS *dp)
|
|
|
|
{
|
|
|
|
DWORD i;
|
|
|
|
|
|
|
|
for(i=0; i < dp->cNamedArgs; i++) {
|
|
|
|
if(dp->rgdispidNamedArgs[i] == DISPID_THIS) {
|
|
|
|
if(V_VT(dp->rgvarg+i) == VT_DISPATCH)
|
|
|
|
return V_DISPATCH(dp->rgvarg+i);
|
|
|
|
|
|
|
|
WARN("This is not VT_DISPATCH\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TRACE("no this passed\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-09-06 16:11:49 +02:00
|
|
|
static HRESULT init_parameters(jsdisp_t *var_disp, FunctionInstance *function, DISPPARAMS *dp,
|
2008-09-10 21:06:25 +02:00
|
|
|
jsexcept_t *ei, IServiceProvider *caller)
|
|
|
|
{
|
|
|
|
parameter_t *param;
|
|
|
|
VARIANT var_empty;
|
|
|
|
DWORD cargs, i=0;
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
V_VT(&var_empty) = VT_EMPTY;
|
2009-09-01 13:26:34 +02:00
|
|
|
cargs = arg_cnt(dp);
|
2008-09-10 21:06:25 +02:00
|
|
|
|
|
|
|
for(param = function->parameters; param; param = param->next) {
|
2009-09-23 16:11:11 +02:00
|
|
|
hres = jsdisp_propput_name(var_disp, param->identifier,
|
2009-09-01 13:26:34 +02:00
|
|
|
i < cargs ? get_arg(dp,i) : &var_empty, ei, caller);
|
2008-09-10 21:06:25 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2009-09-23 16:18:39 +02:00
|
|
|
static HRESULT Arguments_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
|
2009-08-29 00:01:57 +02:00
|
|
|
VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
|
|
|
|
{
|
|
|
|
FIXME("\n");
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const builtin_info_t Arguments_info = {
|
|
|
|
JSCLASS_ARGUMENTS,
|
|
|
|
{NULL, Arguments_value, 0},
|
|
|
|
0, NULL,
|
|
|
|
NULL,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2009-09-23 16:12:12 +02:00
|
|
|
static HRESULT create_arguments(script_ctx_t *ctx, IDispatch *calee, DISPPARAMS *dp,
|
2010-09-06 16:11:49 +02:00
|
|
|
jsexcept_t *ei, IServiceProvider *caller, jsdisp_t **ret)
|
2008-09-10 21:06:43 +02:00
|
|
|
{
|
2010-09-06 16:11:49 +02:00
|
|
|
jsdisp_t *args;
|
2008-09-10 21:06:43 +02:00
|
|
|
VARIANT var;
|
|
|
|
DWORD i;
|
|
|
|
HRESULT hres;
|
|
|
|
|
2009-09-20 20:58:27 +02:00
|
|
|
static const WCHAR caleeW[] = {'c','a','l','l','e','e',0};
|
|
|
|
|
2010-09-06 16:11:49 +02:00
|
|
|
args = heap_alloc_zero(sizeof(jsdisp_t));
|
2009-08-29 00:01:57 +02:00
|
|
|
if(!args)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
hres = init_dispex_from_constr(args, ctx, &Arguments_info, ctx->object_constr);
|
|
|
|
if(FAILED(hres)) {
|
|
|
|
heap_free(args);
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(i=0; i < arg_cnt(dp); i++) {
|
2009-09-23 16:11:11 +02:00
|
|
|
hres = jsdisp_propput_idx(args, i, get_arg(dp,i), ei, caller);
|
2008-09-10 21:06:43 +02:00
|
|
|
if(FAILED(hres))
|
2009-08-29 00:01:57 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(SUCCEEDED(hres)) {
|
|
|
|
V_VT(&var) = VT_I4;
|
|
|
|
V_I4(&var) = arg_cnt(dp);
|
2009-09-23 16:11:11 +02:00
|
|
|
hres = jsdisp_propput_name(args, lengthW, &var, ei, caller);
|
2009-09-20 20:58:27 +02:00
|
|
|
|
|
|
|
if(SUCCEEDED(hres)) {
|
|
|
|
V_VT(&var) = VT_DISPATCH;
|
|
|
|
V_DISPATCH(&var) = calee;
|
2009-09-23 16:11:11 +02:00
|
|
|
hres = jsdisp_propput_name(args, caleeW, &var, ei, caller);
|
2009-09-20 20:58:27 +02:00
|
|
|
}
|
2008-09-10 21:06:43 +02:00
|
|
|
}
|
|
|
|
|
2009-08-29 00:01:57 +02:00
|
|
|
if(FAILED(hres)) {
|
|
|
|
jsdisp_release(args);
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ret = args;
|
|
|
|
return S_OK;
|
2008-09-10 21:06:43 +02:00
|
|
|
}
|
|
|
|
|
2010-09-06 16:11:49 +02:00
|
|
|
static HRESULT create_var_disp(script_ctx_t *ctx, FunctionInstance *function, jsdisp_t *arg_disp,
|
|
|
|
DISPPARAMS *dp, jsexcept_t *ei, IServiceProvider *caller, jsdisp_t **ret)
|
2008-09-10 21:06:09 +02:00
|
|
|
{
|
2010-09-06 16:11:49 +02:00
|
|
|
jsdisp_t *var_disp;
|
2010-07-27 11:31:01 +02:00
|
|
|
VARIANT var;
|
2008-09-10 21:06:09 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
2009-09-23 16:16:43 +02:00
|
|
|
hres = create_dispex(ctx, NULL, NULL, &var_disp);
|
2008-09-10 21:06:09 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2010-09-07 15:41:40 +02:00
|
|
|
var_set_jsdisp(&var, arg_disp);
|
2010-07-27 11:31:01 +02:00
|
|
|
hres = jsdisp_propput_name(var_disp, argumentsW, &var, ei, caller);
|
2008-09-10 21:06:43 +02:00
|
|
|
if(SUCCEEDED(hres))
|
2009-09-23 16:12:12 +02:00
|
|
|
hres = init_parameters(var_disp, function, dp, ei, caller);
|
2008-09-10 21:06:25 +02:00
|
|
|
if(FAILED(hres)) {
|
|
|
|
jsdisp_release(var_disp);
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
2008-09-10 21:06:09 +02:00
|
|
|
*ret = var_disp;
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2009-09-23 16:16:43 +02:00
|
|
|
static HRESULT invoke_source(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj, DISPPARAMS *dp,
|
2008-09-10 21:06:09 +02:00
|
|
|
VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
|
|
|
|
{
|
2010-09-06 16:11:49 +02:00
|
|
|
jsdisp_t *var_disp, *arg_disp;
|
2008-09-10 21:06:09 +02:00
|
|
|
exec_ctx_t *exec_ctx;
|
|
|
|
scope_chain_t *scope;
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
if(!function->source) {
|
|
|
|
FIXME("no source\n");
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
2010-09-07 15:42:01 +02:00
|
|
|
hres = create_arguments(ctx, to_disp(&function->dispex),
|
2010-07-27 11:31:01 +02:00
|
|
|
dp, ei, caller, &arg_disp);
|
2008-09-10 21:06:09 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2010-07-27 11:31:01 +02:00
|
|
|
hres = create_var_disp(ctx, function, arg_disp, dp, ei, caller, &var_disp);
|
|
|
|
if(FAILED(hres)) {
|
|
|
|
jsdisp_release(arg_disp);
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
2008-09-10 21:06:09 +02:00
|
|
|
hres = scope_push(function->scope_chain, var_disp, &scope);
|
|
|
|
if(SUCCEEDED(hres)) {
|
2010-10-14 15:21:13 +02:00
|
|
|
hres = create_exec_ctx(ctx, this_obj, var_disp, scope, FALSE, &exec_ctx);
|
2008-09-10 21:06:09 +02:00
|
|
|
scope_release(scope);
|
|
|
|
}
|
2009-12-31 13:03:01 +01:00
|
|
|
jsdisp_release(var_disp);
|
2010-07-27 11:31:01 +02:00
|
|
|
if(SUCCEEDED(hres)) {
|
2010-09-06 16:11:49 +02:00
|
|
|
jsdisp_t *prev_args;
|
2008-09-10 21:06:09 +02:00
|
|
|
|
2010-07-27 11:31:01 +02:00
|
|
|
prev_args = function->arguments;
|
|
|
|
function->arguments = arg_disp;
|
2010-10-14 15:21:13 +02:00
|
|
|
hres = exec_source(exec_ctx, function->parser, function->source, FALSE, ei, retv);
|
2010-07-27 11:31:01 +02:00
|
|
|
function->arguments = prev_args;
|
|
|
|
|
|
|
|
jsdisp_release(arg_disp);
|
|
|
|
exec_release(exec_ctx);
|
|
|
|
}
|
2008-09-10 21:06:09 +02:00
|
|
|
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
2009-09-23 16:16:43 +02:00
|
|
|
static HRESULT invoke_constructor(script_ctx_t *ctx, FunctionInstance *function, DISPPARAMS *dp,
|
2008-09-10 21:08:26 +02:00
|
|
|
VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
|
|
|
|
{
|
2010-09-06 16:11:49 +02:00
|
|
|
jsdisp_t *this_obj;
|
2009-10-19 20:42:03 +02:00
|
|
|
VARIANT var;
|
2008-09-10 21:08:26 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
2009-09-23 16:16:43 +02:00
|
|
|
hres = create_object(ctx, &function->dispex, &this_obj);
|
2008-09-10 21:08:26 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2010-09-07 15:42:01 +02:00
|
|
|
hres = invoke_source(ctx, function, to_disp(this_obj), dp, &var, ei, caller);
|
2009-07-16 01:17:00 +02:00
|
|
|
if(FAILED(hres)) {
|
|
|
|
jsdisp_release(this_obj);
|
2008-09-10 21:08:26 +02:00
|
|
|
return hres;
|
2009-07-16 01:17:00 +02:00
|
|
|
}
|
2008-09-10 21:08:26 +02:00
|
|
|
|
2009-10-19 20:42:03 +02:00
|
|
|
if(V_VT(&var) == VT_DISPATCH) {
|
|
|
|
jsdisp_release(this_obj);
|
2010-09-07 15:41:40 +02:00
|
|
|
V_VT(retv) = VT_DISPATCH;
|
2009-10-19 20:42:03 +02:00
|
|
|
V_DISPATCH(retv) = V_DISPATCH(&var);
|
|
|
|
}else {
|
|
|
|
VariantClear(&var);
|
2010-09-07 15:41:40 +02:00
|
|
|
var_set_jsdisp(retv, this_obj);
|
2009-10-19 20:42:03 +02:00
|
|
|
}
|
2008-09-10 21:08:26 +02:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2009-09-27 20:59:28 +02:00
|
|
|
static HRESULT invoke_value_proc(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_disp, WORD flags, DISPPARAMS *dp,
|
2008-09-10 21:06:09 +02:00
|
|
|
VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
|
|
|
|
{
|
2009-09-23 16:18:39 +02:00
|
|
|
vdisp_t vthis;
|
2008-09-10 21:06:09 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
if(this_disp)
|
2009-09-23 16:20:27 +02:00
|
|
|
set_disp(&vthis, this_disp);
|
2009-09-27 20:59:28 +02:00
|
|
|
else if(ctx->host_global)
|
|
|
|
set_disp(&vthis, ctx->host_global);
|
2009-09-23 16:20:27 +02:00
|
|
|
else
|
2009-09-27 20:59:28 +02:00
|
|
|
set_jsdisp(&vthis, ctx->global);
|
2008-09-10 21:06:09 +02:00
|
|
|
|
2009-09-23 16:18:39 +02:00
|
|
|
hres = function->value_proc(ctx, &vthis, flags, dp, retv, ei, caller);
|
2008-09-10 21:06:09 +02:00
|
|
|
|
2009-09-23 16:20:27 +02:00
|
|
|
vdisp_release(&vthis);
|
2008-09-10 21:06:09 +02:00
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
2009-09-23 16:16:43 +02:00
|
|
|
static HRESULT call_function(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj, DISPPARAMS *args,
|
2009-08-29 00:02:22 +02:00
|
|
|
VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
|
|
|
|
{
|
2009-09-27 20:59:28 +02:00
|
|
|
if(function->value_proc)
|
|
|
|
return invoke_value_proc(ctx, function, this_obj, DISPATCH_METHOD, args, retv, ei, caller);
|
2009-08-29 00:02:22 +02:00
|
|
|
|
2009-09-27 20:59:28 +02:00
|
|
|
return invoke_source(ctx, function, this_obj, args, retv, ei, caller);
|
2009-08-29 00:02:22 +02:00
|
|
|
}
|
|
|
|
|
2008-09-21 15:47:47 +02:00
|
|
|
static HRESULT function_to_string(FunctionInstance *function, BSTR *ret)
|
|
|
|
{
|
|
|
|
BSTR str;
|
|
|
|
|
2009-09-17 01:04:44 +02:00
|
|
|
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'};
|
|
|
|
|
2008-09-21 15:47:47 +02:00
|
|
|
if(function->value_proc) {
|
2009-09-17 01:04:44 +02:00
|
|
|
DWORD name_len;
|
2008-09-21 15:47:47 +02:00
|
|
|
|
2009-09-17 01:04:44 +02:00
|
|
|
name_len = strlenW(function->name);
|
|
|
|
str = SysAllocStringLen(NULL, sizeof(native_prefixW) + name_len*sizeof(WCHAR) + sizeof(native_suffixW));
|
|
|
|
if(!str)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
memcpy(str, native_prefixW, sizeof(native_prefixW));
|
|
|
|
memcpy(str + sizeof(native_prefixW)/sizeof(WCHAR), function->name, name_len*sizeof(WCHAR));
|
|
|
|
memcpy(str + sizeof(native_prefixW)/sizeof(WCHAR) + name_len, native_suffixW, sizeof(native_suffixW));
|
|
|
|
}else {
|
|
|
|
str = SysAllocStringLen(function->src_str, function->src_len);
|
|
|
|
if(!str)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
2008-09-21 15:47:47 +02:00
|
|
|
|
|
|
|
*ret = str;
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2009-09-23 16:18:39 +02:00
|
|
|
static HRESULT Function_length(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
|
2008-09-09 01:27:01 +02:00
|
|
|
VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
|
|
|
|
{
|
2009-09-23 16:18:39 +02:00
|
|
|
FunctionInstance *This = function_from_vdisp(jsthis);
|
2008-09-09 01:27:38 +02:00
|
|
|
|
|
|
|
TRACE("%p %d\n", This, This->length);
|
|
|
|
|
|
|
|
switch(flags) {
|
|
|
|
case DISPATCH_PROPERTYGET:
|
|
|
|
V_VT(retv) = VT_I4;
|
|
|
|
V_I4(retv) = This->length;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
FIXME("unimplemented flags %x\n", flags);
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
2008-09-09 01:27:01 +02:00
|
|
|
}
|
|
|
|
|
2009-09-23 16:18:39 +02:00
|
|
|
static HRESULT Function_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
|
2008-09-09 01:27:01 +02:00
|
|
|
VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
|
|
|
|
{
|
2008-09-21 15:47:47 +02:00
|
|
|
FunctionInstance *function;
|
|
|
|
BSTR str;
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
2009-09-23 16:18:39 +02:00
|
|
|
if(!(function = function_this(jsthis)))
|
2010-12-28 15:04:59 +01:00
|
|
|
return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
|
2008-09-21 15:47:47 +02:00
|
|
|
|
|
|
|
hres = function_to_string(function, &str);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
if(retv) {
|
|
|
|
V_VT(retv) = VT_BSTR;
|
|
|
|
V_BSTR(retv) = str;
|
|
|
|
}else {
|
|
|
|
SysFreeString(str);
|
|
|
|
}
|
|
|
|
return S_OK;
|
2008-09-09 01:27:01 +02:00
|
|
|
}
|
|
|
|
|
2010-09-06 16:11:49 +02:00
|
|
|
static HRESULT array_to_args(script_ctx_t *ctx, jsdisp_t *arg_array, jsexcept_t *ei, IServiceProvider *caller,
|
2009-08-29 00:02:49 +02:00
|
|
|
DISPPARAMS *args)
|
|
|
|
{
|
|
|
|
VARIANT var, *argv;
|
|
|
|
DWORD length, i;
|
|
|
|
HRESULT hres;
|
|
|
|
|
2009-09-23 16:10:36 +02:00
|
|
|
hres = jsdisp_propget_name(arg_array, lengthW, &var, ei, NULL/*FIXME*/);
|
2009-08-29 00:02:49 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2009-09-23 16:16:43 +02:00
|
|
|
hres = to_uint32(ctx, &var, ei, &length);
|
2009-08-29 00:02:49 +02:00
|
|
|
VariantClear(&var);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
argv = heap_alloc(length * sizeof(VARIANT));
|
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++) {
|
2010-01-25 03:49:49 +01:00
|
|
|
hres = jsdisp_get_idx(arg_array, i, argv+i, ei, caller);
|
|
|
|
if(hres == DISP_E_UNKNOWNNAME)
|
|
|
|
V_VT(argv+i) = VT_EMPTY;
|
|
|
|
else if(FAILED(hres)) {
|
2009-08-29 00:02:49 +02:00
|
|
|
while(i--)
|
|
|
|
VariantClear(argv+i);
|
|
|
|
heap_free(argv);
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
args->cArgs = length;
|
|
|
|
args->rgvarg = argv;
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2009-09-23 16:18:39 +02:00
|
|
|
static HRESULT Function_apply(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
|
2009-08-29 00:02:49 +02:00
|
|
|
VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
|
2008-09-09 01:27:01 +02:00
|
|
|
{
|
2009-08-29 00:02:49 +02:00
|
|
|
FunctionInstance *function;
|
|
|
|
DISPPARAMS args = {NULL,NULL,0,0};
|
|
|
|
DWORD argc, i;
|
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");
|
|
|
|
|
2009-09-24 00:45:42 +02:00
|
|
|
if(!(function = function_this(jsthis)))
|
2010-12-28 15:04:59 +01:00
|
|
|
return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
|
2009-08-29 00:02:49 +02:00
|
|
|
|
|
|
|
argc = arg_cnt(dp);
|
|
|
|
if(argc) {
|
2009-10-30 00:02:13 +01:00
|
|
|
VARIANT *v = get_arg(dp,0);
|
|
|
|
|
|
|
|
if(V_VT(v) != VT_EMPTY && V_VT(v) != VT_NULL) {
|
|
|
|
hres = to_object(ctx, v, &this_obj);
|
|
|
|
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
|
|
|
|
|
|
|
if(V_VT(get_arg(dp,1)) == VT_DISPATCH) {
|
|
|
|
arg_array = iface_to_jsdisp((IUnknown*)V_DISPATCH(get_arg(dp,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) {
|
2009-09-23 16:16:43 +02:00
|
|
|
hres = array_to_args(ctx, arg_array, ei, caller, &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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-23 16:16:43 +02:00
|
|
|
hres = call_function(ctx, function, this_obj, &args, retv, ei, caller);
|
2009-08-29 00:02:49 +02:00
|
|
|
|
|
|
|
if(this_obj)
|
|
|
|
IDispatch_Release(this_obj);
|
|
|
|
for(i=0; i<args.cArgs; i++)
|
|
|
|
VariantClear(args.rgvarg+i);
|
|
|
|
heap_free(args.rgvarg);
|
|
|
|
return hres;
|
2008-09-09 01:27:01 +02:00
|
|
|
}
|
|
|
|
|
2009-09-23 16:18:39 +02:00
|
|
|
static HRESULT Function_call(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
|
2009-08-29 00:02:22 +02:00
|
|
|
VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
|
2008-09-09 01:27:01 +02:00
|
|
|
{
|
2009-08-29 00:02:22 +02:00
|
|
|
FunctionInstance *function;
|
|
|
|
DISPPARAMS args = {NULL,NULL,0,0};
|
|
|
|
IDispatch *this_obj = NULL;
|
|
|
|
DWORD argc;
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
2009-09-24 00:45:42 +02:00
|
|
|
if(!(function = function_this(jsthis)))
|
2010-12-28 15:04:59 +01:00
|
|
|
return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
|
2009-08-29 00:02:22 +02:00
|
|
|
|
|
|
|
argc = arg_cnt(dp);
|
|
|
|
if(argc) {
|
2009-10-30 00:01:49 +01:00
|
|
|
VARIANT *v = get_arg(dp,0);
|
|
|
|
|
|
|
|
if(V_VT(v) != VT_EMPTY && V_VT(v) != VT_NULL) {
|
|
|
|
hres = to_object(ctx, v, &this_obj);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
2009-08-29 00:02:22 +02:00
|
|
|
args.cArgs = argc-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(args.cArgs)
|
|
|
|
args.rgvarg = dp->rgvarg + dp->cArgs - args.cArgs-1;
|
|
|
|
|
2009-09-23 16:16:43 +02:00
|
|
|
hres = call_function(ctx, function, this_obj, &args, retv, ei, caller);
|
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
|
|
|
}
|
|
|
|
|
2009-09-23 16:18:39 +02:00
|
|
|
HRESULT Function_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
|
2008-09-10 21:06:09 +02:00
|
|
|
VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
|
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;
|
|
|
|
}
|
|
|
|
|
2009-09-23 16:18:39 +02:00
|
|
|
function = (FunctionInstance*)jsthis->u.jsdisp;
|
2008-09-10 21:06:09 +02:00
|
|
|
|
|
|
|
switch(flags) {
|
|
|
|
case DISPATCH_METHOD:
|
|
|
|
if(function->value_proc)
|
2009-09-27 20:59:28 +02:00
|
|
|
return invoke_value_proc(ctx, function, get_this(dp), flags, dp, retv, ei, caller);
|
2008-09-10 21:06:09 +02:00
|
|
|
|
2009-09-27 20:59:28 +02:00
|
|
|
return invoke_source(ctx, function, get_this(dp), dp, retv, ei, caller);
|
2008-09-10 21:06:09 +02:00
|
|
|
|
2008-09-21 15:48:11 +02:00
|
|
|
case DISPATCH_PROPERTYGET: {
|
|
|
|
HRESULT hres;
|
|
|
|
BSTR str;
|
|
|
|
|
|
|
|
hres = function_to_string(function, &str);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
V_VT(retv) = VT_BSTR;
|
|
|
|
V_BSTR(retv) = str;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-09-10 21:08:26 +02:00
|
|
|
case DISPATCH_CONSTRUCT:
|
|
|
|
if(function->value_proc)
|
2009-09-27 20:59:28 +02:00
|
|
|
return invoke_value_proc(ctx, function, get_this(dp), flags, dp, retv, ei, caller);
|
2008-09-10 21:08:26 +02:00
|
|
|
|
2009-09-23 16:16:43 +02:00
|
|
|
return invoke_constructor(ctx, function, dp, retv, ei, caller);
|
2008-09-10 21:08:26 +02:00
|
|
|
|
2008-09-10 21:06:09 +02:00
|
|
|
default:
|
|
|
|
FIXME("not implemented flags %x\n", flags);
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
2008-09-09 01:27:01 +02:00
|
|
|
}
|
|
|
|
|
2010-07-27 11:31:01 +02:00
|
|
|
static HRESULT Function_arguments(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
|
|
|
|
DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
|
|
|
|
{
|
|
|
|
FunctionInstance *function = (FunctionInstance*)jsthis->u.jsdisp;
|
|
|
|
HRESULT hres = S_OK;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
|
|
|
switch(flags) {
|
|
|
|
case DISPATCH_PROPERTYGET: {
|
|
|
|
if(function->arguments) {
|
2010-09-07 15:42:34 +02:00
|
|
|
jsdisp_addref(function->arguments);
|
2010-09-07 15:41:40 +02:00
|
|
|
var_set_jsdisp(retv, function->arguments);
|
2010-07-27 11:31:01 +02:00
|
|
|
}else {
|
|
|
|
V_VT(retv) = VT_NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DISPATCH_PROPERTYPUT:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
FIXME("unimplemented flags %x\n", flags);
|
|
|
|
hres = E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
2010-09-06 16:11:49 +02:00
|
|
|
static void Function_destructor(jsdisp_t *dispex)
|
2008-09-09 01:27:01 +02:00
|
|
|
{
|
|
|
|
FunctionInstance *This = (FunctionInstance*)dispex;
|
|
|
|
|
2008-09-09 01:27:57 +02:00
|
|
|
if(This->parser)
|
|
|
|
parser_release(This->parser);
|
|
|
|
if(This->scope_chain)
|
|
|
|
scope_release(This->scope_chain);
|
2008-09-09 01:27:01 +02:00
|
|
|
heap_free(This);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const builtin_prop_t Function_props[] = {
|
2009-08-23 23:38:16 +02:00
|
|
|
{applyW, Function_apply, PROPF_METHOD|2},
|
2010-07-27 11:31:01 +02:00
|
|
|
{argumentsW, Function_arguments, 0},
|
2009-08-23 23:38:16 +02:00
|
|
|
{callW, Function_call, PROPF_METHOD|1},
|
2008-09-09 01:27:01 +02:00
|
|
|
{lengthW, Function_length, 0},
|
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,
|
|
|
|
{NULL, Function_value, 0},
|
|
|
|
sizeof(Function_props)/sizeof(*Function_props),
|
|
|
|
Function_props,
|
|
|
|
Function_destructor,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2009-07-03 02:45:12 +02:00
|
|
|
static HRESULT create_function(script_ctx_t *ctx, const builtin_info_t *builtin_info, DWORD flags,
|
2010-09-06 16:11:49 +02:00
|
|
|
BOOL funcprot, jsdisp_t *prototype, FunctionInstance **ret)
|
2008-09-09 01:27:01 +02:00
|
|
|
{
|
|
|
|
FunctionInstance *function;
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
function = heap_alloc_zero(sizeof(FunctionInstance));
|
|
|
|
if(!function)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2008-09-19 00:46:26 +02:00
|
|
|
if(funcprot)
|
|
|
|
hres = init_dispex(&function->dispex, ctx, &Function_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
|
|
|
|
hres = init_dispex_from_constr(&function->dispex, ctx, &Function_info, ctx->function_constr);
|
2008-09-09 01:27:01 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
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
|
|
|
|
2010-09-06 16:11:49 +02:00
|
|
|
static HRESULT set_prototype(script_ctx_t *ctx, jsdisp_t *dispex, jsdisp_t *prototype)
|
2009-07-12 19:52:31 +02:00
|
|
|
{
|
|
|
|
jsexcept_t jsexcept;
|
|
|
|
VARIANT var;
|
2008-09-10 02:33:15 +02:00
|
|
|
|
2010-09-07 15:41:40 +02:00
|
|
|
var_set_jsdisp(&var, prototype);
|
2009-07-12 19:52:31 +02:00
|
|
|
memset(&jsexcept, 0, sizeof(jsexcept));
|
2008-09-09 01:27:01 +02:00
|
|
|
|
2009-09-23 16:11:11 +02:00
|
|
|
return jsdisp_propput_name(dispex, prototypeW, &var, &jsexcept, NULL/*FIXME*/);
|
2008-09-09 01:27:01 +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
|
|
|
{
|
|
|
|
FunctionInstance *function;
|
|
|
|
HRESULT hres;
|
|
|
|
|
2009-07-12 19:52:31 +02:00
|
|
|
hres = create_function(ctx, builtin_info, flags, FALSE, NULL, &function);
|
2008-09-09 01:27:01 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2010-05-26 19:17:21 +02:00
|
|
|
if(builtin_info) {
|
|
|
|
VARIANT var;
|
|
|
|
|
|
|
|
V_VT(&var) = VT_I4;
|
|
|
|
V_I4(&var) = function->length;
|
|
|
|
hres = jsdisp_propput_const(&function->dispex, lengthW, &var);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(SUCCEEDED(hres))
|
|
|
|
hres = set_prototype(ctx, &function->dispex, prototype);
|
2009-07-12 19:52:31 +02:00
|
|
|
if(FAILED(hres)) {
|
|
|
|
jsdisp_release(&function->dispex);
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
2008-09-09 01:27:01 +02:00
|
|
|
function->value_proc = value_proc;
|
2009-09-17 01:04:44 +02:00
|
|
|
function->name = name;
|
2008-09-09 01:27:01 +02:00
|
|
|
|
|
|
|
*ret = &function->dispex;
|
|
|
|
return S_OK;
|
|
|
|
}
|
2008-09-09 01:27:57 +02:00
|
|
|
|
|
|
|
HRESULT create_source_function(parser_ctx_t *ctx, parameter_t *parameters, source_elements_t *source,
|
2010-09-06 16:11:49 +02:00
|
|
|
scope_chain_t *scope_chain, const WCHAR *src_str, DWORD src_len, jsdisp_t **ret)
|
2008-09-09 01:27:57 +02:00
|
|
|
{
|
|
|
|
FunctionInstance *function;
|
2010-09-06 16:11:49 +02:00
|
|
|
jsdisp_t *prototype;
|
2008-09-09 01:27:57 +02:00
|
|
|
parameter_t *iter;
|
|
|
|
DWORD length = 0;
|
|
|
|
HRESULT hres;
|
|
|
|
|
2008-09-10 21:09:04 +02:00
|
|
|
hres = create_object(ctx->script, NULL, &prototype);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2009-07-12 19:52:31 +02:00
|
|
|
hres = create_function(ctx->script, NULL, PROPF_CONSTR, FALSE, NULL, &function);
|
|
|
|
if(SUCCEEDED(hres)) {
|
|
|
|
hres = set_prototype(ctx->script, &function->dispex, prototype);
|
|
|
|
if(FAILED(hres))
|
|
|
|
jsdisp_release(&function->dispex);
|
|
|
|
}
|
2008-09-10 21:09:04 +02:00
|
|
|
jsdisp_release(prototype);
|
2008-09-09 01:27:57 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
function->source = source;
|
|
|
|
function->parameters = parameters;
|
|
|
|
|
|
|
|
if(scope_chain) {
|
|
|
|
scope_addref(scope_chain);
|
|
|
|
function->scope_chain = scope_chain;
|
|
|
|
}
|
|
|
|
|
|
|
|
parser_addref(ctx);
|
|
|
|
function->parser = ctx;
|
|
|
|
|
|
|
|
for(iter = parameters; iter; iter = iter->next)
|
|
|
|
length++;
|
|
|
|
function->length = length;
|
|
|
|
|
2008-09-21 15:47:47 +02:00
|
|
|
function->src_str = src_str;
|
|
|
|
function->src_len = src_len;
|
|
|
|
|
2008-09-09 01:27:57 +02:00
|
|
|
*ret = &function->dispex;
|
|
|
|
return S_OK;
|
|
|
|
}
|
2008-09-19 00:46:26 +02:00
|
|
|
|
2009-12-10 01:14:58 +01:00
|
|
|
static HRESULT construct_function(script_ctx_t *ctx, DISPPARAMS *dp, jsexcept_t *ei, IDispatch **ret)
|
|
|
|
{
|
|
|
|
function_expression_t *expr;
|
|
|
|
WCHAR *str = NULL, *ptr;
|
|
|
|
DWORD argc, len = 0, l;
|
|
|
|
parser_ctx_t *parser;
|
2010-09-06 16:11:49 +02:00
|
|
|
jsdisp_t *function;
|
2009-12-10 01:14:58 +01:00
|
|
|
BSTR *params = NULL;
|
2009-12-10 23:56:21 +01:00
|
|
|
int i=0, 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};
|
|
|
|
|
|
|
|
argc = arg_cnt(dp);
|
|
|
|
if(argc) {
|
|
|
|
params = heap_alloc(argc*sizeof(BSTR));
|
|
|
|
if(!params)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
if(argc > 2)
|
|
|
|
len = (argc-2)*2; /* separating commas */
|
|
|
|
for(i=0; i < argc; i++) {
|
|
|
|
hres = to_string(ctx, get_arg(dp,i), ei, params+i);
|
|
|
|
if(FAILED(hres))
|
|
|
|
break;
|
|
|
|
len += SysStringLen(params[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(SUCCEEDED(hres)) {
|
|
|
|
len += (sizeof(function_anonymousW) + sizeof(function_beginW) + sizeof(function_endW)) / sizeof(WCHAR);
|
|
|
|
str = heap_alloc(len*sizeof(WCHAR));
|
|
|
|
if(str) {
|
|
|
|
memcpy(str, function_anonymousW, sizeof(function_anonymousW));
|
|
|
|
ptr = str + sizeof(function_anonymousW)/sizeof(WCHAR);
|
|
|
|
if(argc > 1) {
|
|
|
|
while(1) {
|
|
|
|
l = SysStringLen(params[j]);
|
|
|
|
memcpy(ptr, params[j], l*sizeof(WCHAR));
|
|
|
|
ptr += l;
|
|
|
|
if(++j == argc-1)
|
|
|
|
break;
|
|
|
|
*ptr++ = ',';
|
|
|
|
*ptr++ = ' ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
memcpy(ptr, function_beginW, sizeof(function_beginW));
|
|
|
|
ptr += sizeof(function_beginW)/sizeof(WCHAR);
|
|
|
|
if(argc) {
|
|
|
|
l = SysStringLen(params[argc-1]);
|
|
|
|
memcpy(ptr, params[argc-1], l*sizeof(WCHAR));
|
|
|
|
ptr += l;
|
|
|
|
}
|
|
|
|
memcpy(ptr, function_endW, sizeof(function_endW));
|
|
|
|
|
|
|
|
TRACE("%s\n", debugstr_w(str));
|
|
|
|
}else {
|
|
|
|
hres = E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while(--i >= 0)
|
|
|
|
SysFreeString(params[i]);
|
|
|
|
heap_free(params);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
hres = script_parse(ctx, str, NULL, &parser);
|
|
|
|
heap_free(str);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
if(!parser->source || !parser->source->functions || parser->source->functions->next || parser->source->variables) {
|
|
|
|
ERR("Invalid parser result!\n");
|
|
|
|
parser_release(parser);
|
|
|
|
return E_UNEXPECTED;
|
|
|
|
}
|
|
|
|
expr = parser->source->functions->expr;
|
|
|
|
|
|
|
|
hres = create_source_function(parser, expr->parameter_list, expr->source_elements, NULL, expr->src_str,
|
|
|
|
expr->src_len, &function);
|
|
|
|
parser_release(parser);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-12-10 01:14:27 +01:00
|
|
|
static HRESULT FunctionConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
|
|
|
|
VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
|
|
|
|
{
|
2009-12-10 01:14:58 +01:00
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
|
|
|
switch(flags) {
|
|
|
|
case DISPATCH_CONSTRUCT: {
|
|
|
|
IDispatch *ret;
|
|
|
|
|
|
|
|
hres = construct_function(ctx, dp, ei, &ret);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
V_VT(retv) = VT_DISPATCH;
|
|
|
|
V_DISPATCH(retv) = ret;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
FIXME("unimplemented flags %x\n", flags);
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
2009-12-10 01:14:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT FunctionProt_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
|
|
|
|
VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
|
|
|
FunctionInstance *prot, *constr;
|
|
|
|
HRESULT hres;
|
|
|
|
|
2009-09-17 01:04:44 +02:00
|
|
|
static const WCHAR FunctionW[] = {'F','u','n','c','t','i','o','n',0};
|
|
|
|
|
2009-07-12 19:52:31 +02:00
|
|
|
hres = create_function(ctx, NULL, PROPF_CONSTR, TRUE, object_prototype, &prot);
|
2008-09-19 00:46:26 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
prot->value_proc = FunctionProt_value;
|
2009-09-17 01:04:44 +02:00
|
|
|
prot->name = prototypeW;
|
2008-09-19 00:46:26 +02:00
|
|
|
|
2009-10-13 22:08:43 +02:00
|
|
|
hres = create_function(ctx, NULL, PROPF_CONSTR|1, TRUE, &prot->dispex, &constr);
|
2009-07-12 19:52:31 +02:00
|
|
|
if(SUCCEEDED(hres)) {
|
|
|
|
constr->value_proc = FunctionConstr_value;
|
2009-09-17 01:04:44 +02:00
|
|
|
constr->name = FunctionW;
|
2009-07-12 19:52:31 +02:00
|
|
|
hres = set_prototype(ctx, &constr->dispex, &prot->dispex);
|
|
|
|
if(FAILED(hres))
|
|
|
|
jsdisp_release(&constr->dispex);
|
|
|
|
}
|
2008-09-19 00:46:26 +02:00
|
|
|
jsdisp_release(&prot->dispex);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
ctx->function_constr = &constr->dispex;
|
2009-07-12 19:52:31 +02:00
|
|
|
return S_OK;
|
2008-09-19 00:46:26 +02:00
|
|
|
}
|