double simpsons_approx(int fun,double lo,double hi){//do a simpson's approximation on the appropriate function over the specified interval double p1=(hi-lo)/6.0; double pLo=call_function(fun,lo); double pAv=4.0 * call_function(fun,(lo+hi)/2 ); double pHi=call_function(fun,hi); return p1*( pLo+pAv+pHi ); }
void create_object(struct object *ob) { int i; if (!(ob->flags & O_CREATED)) { ob->flags |= O_CREATED; ob->created = current_time; for (i = 0; i < (int)ob->prog->num_inherited; i++) if (!(ob->prog->inherit[i].type & TYPE_MOD_SECOND)) { if (ob->prog->inherit[i].prog->ctor_index != (unsigned short) -1) { call_function(ob, i, (unsigned int)ob->prog->inherit[i].prog->ctor_index, 0); pop_stack(); } } if (search_for_function("create", ob->prog)) { call_function(ob, function_inherit_found, (unsigned int)function_index_found, 0); pop_stack(); } } }
const SpObject *SpVM::eval(const SpExpr *expr, SpEnv *env) { // check whether this expression is an atom or a function call if (expr->head()->type() == TOKEN_FUNCTION_CALL) { // this is a function call expression std::string func_name(expr->head()->value()); // we find the function associated with this name in the given environment const SpObject *obj = resolve(func_name, env); if (obj == NULL || obj->type() != T_FUNCTION) RUNTIME_ERROR_F("'%s' is not a function", func_name.c_str()); // now call the function return call_function(func_name, static_cast<const SpFunction *>(obj->self()), expr, env); } else { // evaluate this atom std::string val = expr->head()->value(); switch (expr->head()->type()) { case TOKEN_NAME: { const SpObject *obj = resolve(val, env); if (obj == NULL) RUNTIME_ERROR_F("Undeclared variable '%s'", val.c_str()); return obj->shallow_copy(); } case TOKEN_NUMERIC: return new SpIntValue(atoi(val.c_str())); case TOKEN_CLOSURE: // the closure expression is stored in the first argument of the // object expression return new SpRefObject(new SpClosure(ArgList(), NULL, *expr->cbegin())); default: RUNTIME_ERROR("Unsupported operation"); } } }
int get_type(char *format, va_list args) { int id; int count[1]; count[0] = 0; count[1] = 0; while (format[count[1]] != '\0') { id = -1; if (format[count[1]] == '%') { id = get_id(count[1], format, "csdioxXbpSuf"); if (id >= 0) { count[0] += call_function(id, args); count[1]++; } count[1]++; } else get_type_next(format, count); } return (count[0]); }
/** * \brief Calls the on_closed() method of the object on top of the stack. */ void LuaContext::on_closed() { if (find_method("on_closed")) { call_function(1, 0, "on_closed"); } }
int32 interpreter::get_function_value(int32 f, uint32 param_count) { int32 result; if(!f) { params.clear(); return 0; } no_action++; call_depth++; if (call_function(f, param_count, 1)) { if (lua_isboolean(current_state, -1)) result = lua_toboolean(current_state, -1); else result = lua_tointeger(current_state, -1); lua_pop(current_state, 1); no_action--; call_depth--; if(call_depth == 0) { pduel->release_script_group(); pduel->restore_assumes(); } return result; } no_action--; call_depth--; if(call_depth == 0) { pduel->release_script_group(); pduel->restore_assumes(); } return OPERATION_FAIL; }
int32 interpreter::check_condition(int32 f, uint32 param_count) { int32 result; if(!f) { params.clear(); return TRUE; } no_action++; call_depth++; if (call_function(f, param_count, 1)) { result = lua_toboolean(current_state, -1); lua_pop(current_state, 1); no_action--; call_depth--; if(call_depth == 0) { pduel->release_script_group(); pduel->restore_assumes(); } return result; } no_action--; call_depth--; if(call_depth == 0) { pduel->release_script_group(); pduel->restore_assumes(); } return OPERATION_FAIL; }
bool evaluate_variable(const object& variable_name) { object result; // std::cout << "GET VARIABLE" << std::endl; if(get_variable(variable_name.data.variable, &result) == OK) { // std::cout << "GET VARIABLE" << std::endl; // Auto call zero argument functions when they are found. if(result.type == FUNCTION) { if(result.data.function->arguments.size() == 1) { object arguments; // empty, won't be used by call_function so no need to initialize call_function(result, result, arguments); } } optic_stack.push_back(result); // result is already a copy, no need to copy again return true; } else { out() << "Variable " << reverse_variable_name_lookup[variable_name.data.variable] << " not found." << std::endl; clear_stack(); return false; } }
/** * \brief Executes the callback of a timer. * * Then, if the callback returns \c true, the timer is rescheduled, * otherwise it is discarded. * * Does nothing if the timer is already finished. * * \param timer The timer to execute. */ void LuaContext::do_timer_callback(Timer& timer) { Debug::check_assertion(timer.is_finished(), "This timer is still running"); const std::map<Timer*, LuaTimerData>::iterator it = timers.find(&timer); if (it != timers.end() && it->second.callback_ref != LUA_REFNIL) { const int callback_ref = it->second.callback_ref; push_callback(callback_ref); const bool success = call_function(0, 1, "timer callback"); bool repeat = false; if (success) { repeat = lua_isboolean(l, -1) && lua_toboolean(l, -1); lua_pop(l, 1); } if (repeat) { // The callback returned true: reschedule the timer. timer.set_expiration_date(timer.get_expiration_date() + timer.get_initial_duration()); if (timer.is_finished()) { // Already finished: this is possible if the duration is smaller than // the main loop stepsize. do_timer_callback(timer); } } else { cancel_callback(callback_ref); it->second.callback_ref = LUA_REFNIL; timers_to_remove.push_back(&timer); } } }
int main() { lua_State *L = luaL_newstate(); call_function(L); lua_close(L); return 0; }
static HRESULT Function_call(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r) { FunctionInstance *function; IDispatch *this_obj = NULL; unsigned cnt = 0; HRESULT hres; TRACE("\n"); if(!(function = function_this(jsthis))) return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL); if(argc) { if(!is_undefined(argv[0]) && !is_null(argv[0])) { hres = to_object(ctx, argv[0], &this_obj); if(FAILED(hres)) return hres; } cnt = argc-1; } hres = call_function(ctx, function, this_obj, cnt, argv+1, r); if(this_obj) IDispatch_Release(this_obj); return hres; }
/** * \brief Executes the callback of a timer. * * Then, if the callback returns \c true, the timer is rescheduled, * otherwise it is discarded. * * Does nothing if the timer is already finished. * * \param timer The timer to execute. */ void LuaContext::do_timer_callback(const TimerPtr& timer) { Debug::check_assertion(timer->is_finished(), "This timer is still running"); auto it = timers.find(timer); if (it != timers.end() && !it->second.callback_ref.is_empty()) { ScopedLuaRef& callback_ref = it->second.callback_ref; push_ref(l, callback_ref); const bool success = call_function(0, 1, "timer callback"); bool repeat = false; if (success) { repeat = lua_isboolean(l, -1) && lua_toboolean(l, -1); lua_pop(l, 1); } if (repeat) { // The callback returned true: reschedule the timer. timer->set_expiration_date(timer->get_expiration_date() + timer->get_initial_duration()); if (timer->is_finished()) { // Already finished: this is possible if the duration is smaller than // the main loop stepsize. do_timer_callback(timer); } } else { callback_ref.clear(); timers_to_remove.push_back(timer); } } }
static VALUE epic_call (VALUE module, VALUE string) { char *funcval; RUBY_STARTUP funcval = call_function(my_string, ""); return rb_str_new(funcval, strlen(funcval)); }
static void do_test_run(struct code_chunks *chunk) { /* type-1 function */ char df1[] = { 0x64, 0x00, 0x00, 0x00, /* 100 */ 'x', 0x00, 0x00, 0x00, /* 'x' */ 0xC8, 0x00, 0x00, 0x00, /* 200 */ }; /* type-2 function */ char *name = "mygirl"; char df2[] = { 0x00, 0x00, 0x00, 0x00, /* name */ 0x04, 0x00, 0x00, 0x00 /* 4 */ }; /* type-3 function */ int fibo; char df3[] = { 0x0f, 0x00, 0x00, 0x00, /* 15 */ 0x00, 0x00, 0x00, 0x00 /* fibo */ }; char number[15]; char df4[] = { 0xde, 0xad, 0xbe, 0xef, 0x00, 0x00, 0x00, 0x00 /* number */ }; char df5[] = { }; ptr2char(df2, name); ptr2char((char *)df3 + 4, &fibo); ptr2char((char *)df4 + 4, number); call_function(chunk, FUNC_TYPE1, df1); call_function(chunk, FUNC_TYPE2, df2); call_function(chunk, FUNC_TYPE3, df3); printf("%s: fibo value returned: %d\n", __func__, fibo); call_function(chunk, FUNC_TYPE5, df5); call_function(chunk, FUNC_TYPE4, df4); printf("%s: original: %d, reversed: %s\n", __func__, *(int *)df4, number); }
uint64_t query(factor Factor) const { const auto Diff = call_function(QueryPerformanceCounter) - m_StartTime; const auto Frequency = get_frequency(); const auto Whole = Diff / Frequency; const auto Fraction = Diff % Frequency; return Whole * Factor + (Fraction * Factor) / Frequency; }
/** * @since 2016-11-20 */ Object tm_load_module(Object file, Object code, Object name) { Object mod = module_new(file, name, code); Object fnc = func_new(mod, NONE_OBJECT, NULL); GET_FUNCTION(fnc)->code = (unsigned char*) GET_STR(code); GET_FUNCTION(fnc)->name = string_new("#main"); call_function(fnc); return GET_MODULE(mod)->globals; }
/** * @brief Calls the on_draw() method of the object on top of the stack. * @param dst_surface The destination surface. */ void LuaContext::on_draw(Surface& dst_surface) { if (find_method("on_draw")) { push_surface(l, dst_surface); call_function(2, 0, "on_draw"); } }
bool LuaContext::do_file_if_exists(lua_State* l, const std::string& script_name) { if(load_file_if_exists(l, script_name)) { call_function(l, 0, 0, script_name); return true; } return false; }
/** * @brief Calls a function stored in the registry with a reference and * releases this reference. * @param callback_ref Reference of the function to call (if LUA_REFNIL, * nothing is done). */ void LuaContext::do_callback(int callback_ref) { if (callback_ref != LUA_REFNIL) { push_callback(callback_ref); call_function(0, 0, "callback"); destroy_ref(callback_ref); } }
static HRESULT Function_apply(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller) { FunctionInstance *function; DISPPARAMS args = {NULL,NULL,0,0}; DWORD argc, i; IDispatch *this_obj = NULL; HRESULT hres = S_OK; TRACE("\n"); if(!(function = function_this(jsthis))) return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL); argc = arg_cnt(dp); if(argc) { 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; } } if(argc >= 2) { jsdisp_t *arg_array = NULL; if(V_VT(get_arg(dp,1)) == VT_DISPATCH) { arg_array = iface_to_jsdisp((IUnknown*)V_DISPATCH(get_arg(dp,1))); if(arg_array && (!is_class(arg_array, JSCLASS_ARRAY) && !is_class(arg_array, JSCLASS_ARGUMENTS) )) { jsdisp_release(arg_array); arg_array = NULL; } } if(arg_array) { hres = array_to_args(ctx, arg_array, ei, caller, &args); jsdisp_release(arg_array); }else { FIXME("throw TypeError\n"); hres = E_FAIL; } } if(SUCCEEDED(hres)) hres = call_function(ctx, function, this_obj, &args, retv, ei, caller); if(this_obj) IDispatch_Release(this_obj); for(i=0; i<args.cArgs; i++) VariantClear(args.rgvarg+i); heap_free(args.rgvarg); return hres; }
void ApplicationWindow::onCheckForNewVersionResult(VersionInfo::VersionInfo _versioninfo) { if (_versioninfo.appVersionBuild == 0) { // no connect to update server... std::unique_lock<std::mutex> lock(lockGuiUpdate); call_function("Application.newVersionCheckFailed"); } else if (_versioninfo.appVersionBuild > versionInfoApp.appVersionBuild) { // new version is ready for download std::unique_lock<std::mutex> lock(lockGuiUpdate); call_function("Application.newVersionAvailable", sciter::value(currentVersionBinarySource), sciter::value(_versioninfo.appVersion), sciter::value(std::to_string(_versioninfo.appVersionBuild))); } else { call_function("Application.versionIsUpToDate", sciter::value(_versioninfo.appVersion), sciter::value(std::to_string(_versioninfo.appVersionBuild))); } }
/** * 検索して見つかった場合/見つからなかった場合は * 1.true/falseを返す * 2.index/nullを返す */ int map_index_of(Value *vret, Value *v, RefNode *node) { RefMap *rm = Value_vp(*v); int ret_index = FUNC_INT(node); Value v1 = v[1]; RefNode *type = Value_type(v1); int i; RefNode *fn_eq = Hash_get_p(&type->u.c.h, fs->symbol_stock[T_EQ]); if (fn_eq == NULL) { throw_error_select(THROW_NO_MEMBER_EXISTS__NODE_REFSTR, type, fs->symbol_stock[T_EQ]); return FALSE; } rm->lock_count++; for (i = 0; i < rm->entry_num; i++) { HashValueEntry *ep = rm->entry[i]; for (; ep != NULL; ep = ep->next) { Value va = ep->val; if (Value_type(va) == type) { if (type == fs->cls_str) { if (refstr_eq(Value_vp(v1), Value_vp(va))) { break; } } else { Value_push("vv", v1, va); if (!call_function(fn_eq, 1)) { goto ERROR_END; } fg->stk_top--; if (Value_bool(*fg->stk_top)) { unref(*fg->stk_top); if (ret_index) { *vret = Value_cp(ep->key); } else { *vret = VALUE_TRUE; } return TRUE; } } } } } if (!ret_index) { *vret = VALUE_FALSE; } rm->lock_count--; return TRUE; ERROR_END: rm->lock_count--; return FALSE; }
static syn_node_t * identifier() { switch (current_tok) { case TOK_LBRACKET: return access_array(); case TOK_LPAR: return call_function(); default: return syn_node_id_new(lex_item_prev.item); } }
int main( int argc, char * argv[] ) { char * buffer; buffer = (char *)malloc( 64 ); strcpy( buffer, "abc" ); printf("%s\n", buffer ); call_function(); printf("returned from call_function & do_nothing.\n"); free( buffer ); return 0; }
static HRESULT Function_apply(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r) { FunctionInstance *function; jsval_t *args = NULL; unsigned i, cnt = 0; IDispatch *this_obj = NULL; HRESULT hres = S_OK; TRACE("\n"); if(!(function = function_this(jsthis))) return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL); if(argc) { if(!is_undefined(argv[0]) && !is_null(argv[0])) { hres = to_object(ctx, argv[0], &this_obj); if(FAILED(hres)) return hres; } } if(argc >= 2) { jsdisp_t *arg_array = NULL; if(is_object_instance(argv[1])) { arg_array = iface_to_jsdisp((IUnknown*)get_object(argv[1])); if(arg_array && (!is_class(arg_array, JSCLASS_ARRAY) && !is_class(arg_array, JSCLASS_ARGUMENTS) )) { jsdisp_release(arg_array); arg_array = NULL; } } if(arg_array) { hres = array_to_args(ctx, arg_array, &cnt, &args); jsdisp_release(arg_array); }else { FIXME("throw TypeError\n"); hres = E_FAIL; } } if(SUCCEEDED(hres)) hres = call_function(ctx, function, this_obj, cnt, args, r); if(this_obj) IDispatch_Release(this_obj); for(i=0; i < cnt; i++) jsval_release(args[i]); heap_free(args); return hres; }
void redirect_stdout(char *program, char* redirect_type, char *file_name) { int status; //exec command pid_t pID = fork(); if (pID == 0) { // child int out_file_fd; if (strcmp(redirect_type, ">") == 0) { out_file_fd = open(file_name, O_RDWR|O_CREAT|O_TRUNC, S_IRWXU|S_IRWXG|S_IRWXO); } else if (strcmp(redirect_type, ">>") == 0) { out_file_fd = open(file_name, O_RDWR|O_CREAT|O_APPEND, S_IRWXU|S_IRWXG|S_IRWXO); } if (-1 == out_file_fd) { printf("error opening file:%s\n", file_name); exit(4); } //redirect stdout to file if (dup2(out_file_fd, fileno(stdout)) == -1) { printf("error redirectiong stdout to file\n"); exit(5); } char * args[MAX_PARAM_NUMBER]; create_arguments(args, program); if(is_builtin(program)==1) { char * tem[MAX_PARAM_NUMBER]; tem[0]=PARAMS[1]; call_function(PARAMS[0],tem,N_params-1); } else if(execvp(args[0],args)==-1) printf("%s\n","cannot extec"); fflush(stdout); close(out_file_fd); exit(0); } else if (pID < 0) { // failed to fork exit(1); // Throw exception } else { // parent // Code only executed by parent process pID = wait(&status); last_process_status_code = status; } }
static XS (XS_call) { int foo = 0; char* retval=NULL; char* arg=NULL; dXSARGS; for (foo=0; foo<items; foo++) { arg = malloc_strdup((char*)SvPV_nolen(ST(foo))); retval = (char*)call_function(arg, ""); XST_mPV(foo, retval); new_free(&arg); new_free(&retval); } XSRETURN(items); }
/** * @brief Notifies the object on top of the stack * that a keyboard key was just pressed * (including if it is a directional key or a character). * @param event The corresponding input event. * @return \c true if the event was handled and should stop being propagated. */ bool LuaContext::on_key_pressed(InputEvent& event) { bool handled = false; if (find_method("on_key_pressed")) { const std::string& key_name = InputEvent::get_keyboard_key_name(event.get_keyboard_key()); if (!key_name.empty()) { // This key exists in the KQ API. push_string(l, key_name); lua_newtable(l); /* if (event.is_with_shift()) { lua_pushboolean(l, 1); lua_setfield(l, -2, "shift"); } if (event.is_with_control()) { lua_pushboolean(l, 1); lua_setfield(l, -2, "control"); } if (event.is_with_alt()) { lua_pushboolean(l, 1); lua_setfield(l, -2, "alt"); } */ bool success = call_function(3, 1, "on_key_pressed"); if (!success) { // Something was wrong in the script: don't propagate the input to other objects. handled = true; } else { handled = lua_toboolean(l, -1); lua_pop(l, 1); } } else { // The method exists but the key is unknown. lua_pop(l, 2); // Pop the object and the method. } } return handled; }
Object obj_mod(Object a, Object b) { if (a.type == b.type && a.type == TYPE_NUM) { return tm_number((long) GET_NUM(a) % (long) GET_NUM(b)); } else if (a.type == TYPE_STR) { Object *__mod__ = get_builtin("__mod__"); if (__mod__ == NULL) { tm_raise("__mod__ is not defined"); } else { arg_start(); arg_push(a); arg_push(b); return call_function(*__mod__); } } tm_raise("obj_mod: can not module %o and %o", a, b); return NONE_OBJECT; }
static int log_msg(lua_State *L, int level) { static int loaded = FALSE; int n = lua_gettop(L); lua_Debug caller; const char *file, *func; int line; int top; top = lua_gettop(L); if (!loaded) { luaopen_string(L); loaded = TRUE; } if (lua_isuserdata(L, 1)) { lua_remove(L, 1); /* remove self if any */ n--; } if (n > 1) if (call_function(L, "string", "format") != 0) goto out; lua_getstack(L, 1, &caller); if (lua_getinfo(L, "Snl", &caller)) { func = caller.name ? caller.name : "<lua-function>"; file = caller.source ? caller.source : "<lua-source>"; line = caller.currentline; } else { func = "<lua-function>"; line = 0; file = "<lua-source>"; } mrp_log_msg(level, file, line, func, "%s", lua_tostring(L, 1)); out: lua_settop(L, top); return 0; }