/// Calls a VimL function with the given arguments. /// On VimL error: Returns a generic error; v:errmsg is not updated. /// /// @param fname Function to call /// @param args Function arguments packed in an Array /// @param[out] err Error details, if any /// @return Result of the function call Object nvim_call_function(String fname, Array args, Error *err) { Object rv = OBJECT_INIT; if (args.size > MAX_FUNC_ARGS) { api_set_error(err, Validation, _("Function called with too many arguments.")); return rv; } // Convert the arguments in args from Object to typval_T values typval_T vim_args[MAX_FUNC_ARGS + 1]; size_t i = 0; // also used for freeing the variables for (; i < args.size; i++) { if (!object_to_vim(args.items[i], &vim_args[i], err)) { goto free_vim_args; } } try_start(); // Call the function typval_T rettv; int dummy; int r = call_func((char_u *)fname.data, (int)fname.size, &rettv, (int)args.size, vim_args, NULL, curwin->w_cursor.lnum, curwin->w_cursor.lnum, &dummy, true, NULL, NULL); if (r == FAIL) { api_set_error(err, Exception, _("Error calling function.")); } if (!try_end(err)) { rv = vim_to_object(&rettv); } clear_tv(&rettv); free_vim_args: while (i > 0) { clear_tv(&vim_args[--i]); } return rv; }
void test_assert_util_abort(const char * function_name , void call_func (void *) , void * arg) { bool util_abort_intercepted = false; { jmp_buf * context = util_abort_test_jump_buffer(); util_abort_test_set_intercept_function( function_name ); if (setjmp(*context) == 0) call_func( arg ); else util_abort_intercepted = true; util_abort_test_set_intercept_function( NULL ); } if (!util_abort_intercepted) { fprintf(stderr,"Expected call to util_abort() from:%s missing \n",function_name); test_assert_true( util_abort_intercepted ); } }
double function() { std::string name = cur_val.string_value; std::string args; if (set_args(args)) { double result; Token_value tmp; switch (tmp = get_token()) { case ASSIGN: result = def_func(name, args); curr_tok = PRINT; break; default: result = call_func(name, args); curr_tok = tmp; break; } return result; } else return error(""); }
double noinline bench_M (pixman_op_t op, pixman_image_t * src_img, pixman_image_t * mask_img, pixman_image_t * dst_img, int64_t n, pixman_composite_func_t func) { int64_t i; int x = 0; for (i = 0; i < n; i++) { if (++x >= 64) x = 0; call_func (func, op, src_img, mask_img, dst_img, x, 0, x, 0, 1, 0, WIDTH - 64, HEIGHT); } return (double)n * (WIDTH - 64) * HEIGHT; }
/* Interposed malloc which optionally calls another function. */ void * malloc (size_t size) { interposed_malloc_called = true; static void *(*original_malloc) (size_t); if (original_malloc == NULL) { static bool in_initialization; if (in_initialization) { const char *message = "error: malloc called recursively during initialization\n"; (void) write (STDOUT_FILENO, message, strlen (message)); _exit (2); } in_initialization = true; original_malloc = (__typeof (original_malloc)) dlsym (RTLD_NEXT, "malloc"); if (original_malloc == NULL) { const char *message = "error: dlsym for malloc failed\n"; (void) write (STDOUT_FILENO, message, strlen (message)); _exit (2); } } if (call_function) { call_function = false; call_func (DSO1, FUNC1); call_function = true; } return original_malloc (size); }
void atom(parser &p, float &value) { int id = 0, last = 0; MSG("atom = %s\n", p.tokens[p.index].data); switch(p.tokens[p.index].type) { case IDENTIFIER: if((id = find_func(p, p.tokens[p.index].data)) != -1) { last = p.index; value = call_func(p, id); } else { if(p.tokens[p.index+1].type == PARENTHESIS_LEFT) MSG("unknown func\n"); } break; case FLOAT_NUMBER: value = (float)atof((const char*)p.tokens[p.index].data); MSG("found float num\n"); p.index++; break; case INT_NUMBER: value = (float)atoi((const char*)p.tokens[p.index].data); MSG("found int number\n"); MSG("num = %s\n", p.tokens[p.index].data); p.index++; break; case PARENTHESIS_RIGHT: return; default: MSG("unknow\n"); }; }
void call_tuple(Tuple t, std::index_sequence<Indices...> s) { call_func(t, s); }
void cleanupHook() { os::MutexLock lock(m); call_func(L, "cleanupHook", this); }
void stopHook() { os::MutexLock lock(m); call_func(L, "stopHook", this); }
void updateHook() { os::MutexLock lock(m); call_func(L, "updateHook", this); }
bool startHook() { os::MutexLock lock(m); return call_func(L, "startHook", this); }
main() { int type; double op2; double tmp, tmp2; extern int sp; char s[MAXOP]; while ((type = getop(s)) != EOF) { switch (type) { case NUMBER: push(atof(s)); break; case '+': push(pop() + pop()); break; case '*': push(pop() * pop()); break; case '-': op2 = pop(); push(pop() - op2); break; case '/': op2 = pop(); if (op2 != 0.0) push(pop() / op2); else printf("error: zero divisor\n"); break; case '%': op2 = pop(); push((int) pop() % (int) op2); break; case '\n': printf("\t%.8g\n", pop()); break; case IDENTIFIER: if (s[1] == '\0') { switch (s[0]) { case 'p': tmp = pop(); printf("\t%.8g\n", tmp); push(tmp); break; case 'c': tmp = pop(); push(tmp); push(tmp); break; case 'x': tmp = pop(); tmp2 = pop(); push(tmp); push(tmp2); break; case 'e': while (sp != 0) pop(); break; default: goto FunctionCall; } goto End; } FunctionCall: call_func(s); break; case ERROR: break; default: printf("error: unknown command %s\n", s); break; } End: ; } return 0; }
static int lua_cfunc(lua_State* L) { return call_func(L, std::index_sequence_for<Args...>{}); }
int main(int argc, char ** argv){ void (*func[])(char * w, int a) = {f1,f2,f3}; int fg = argc - 1; call_func(fg,func,"called",3); return 0; }
itpType *interpret(nodeType *p) { int nodeSize; int len, n; itpType *pret, *pret1, *pret2, *pret3, *pret4; DBG("come in function\n"); if (p == NULL) return NULL; DBG_NODE(p); pret = malloc(sizeof(itpType)); if (pret == NULL) { yyerror("interpret, malloc pret failed\n"); return NULL; } pret->type = typeLong; pret->ival = 1; /* default to return true */ switch(p->type) { case typeLongCon: pret->ival = p->longCon.var; pret->type = typeLong; DBG_INTERPRET(pret); return pret; break; case typeStrCon: len = strlen(p->strCon.ptr); pret->pstr = malloc(len + 1); if (pret->pstr == NULL) { yyerror("interpret, typeStrCon malloc failed\n"); free_itpType(pret); return NULL; } strncpy(pret->pstr, p->strCon.ptr, len); pret->pstr[len] = '\0'; pret->type = typeStr; DBG_INTERPRET(pret); return pret; break; case typeVar: nodeSize = sizeof(symType); memcpy(pret, &symtab[p->var.index], nodeSize); if (pret->type == typeStr) { len = strlen(pret->pstr); pret->pstr = malloc(len + 1); if (pret->pstr == NULL) { yyerror("interpret, typeVar malloc failed\n"); free_itpType(pret); return NULL; } strncpy(pret->pstr, symtab[p->var.index].pstr, len); pret->pstr[len] = '\0'; } DBG_INTERPRET(pret); return pret; break; case typeOpr: switch(p->opr.name) { case WHILE: pret1 = interpret(p->opr.op[0]); if (pret1 == NULL) { free_itpType(pret); return NULL; } while ((pret1->type == typeLong && pret1->ival) || (pret1->type == typeStr && pret1->pstr)) { pret2 = interpret(p->opr.op[1]); free_itpType(pret1); free_itpType(pret2); pret1 = interpret(p->opr.op[0]); if (pret1 == NULL) { free_itpType(pret); return NULL; } } free_itpType(pret1); DBG_INTERPRET(pret); return pret; break; case IF: pret1 = interpret(p->opr.op[0]); if (pret1 == NULL) return NULL; if ((pret1->type == typeLong && pret1->ival) || (pret1->type == typeStr && pret1->pstr)) { pret2 = interpret(p->opr.op[1]); free_itpType(pret1); free_itpType(pret2); } else if (p->opr.number > 2) { pret2 = interpret(p->opr.op[2]); free_itpType(pret1); free_itpType(pret2); } DBG_INTERPRET(pret); return pret; case PRINT: pret1 = interpret(p->opr.op[0]); if (pret1 == NULL) return NULL; if (pret1->type == typeLong) PRINTLONG(pret1); else if (pret1->type == typeStr) PRINTSTR(pret1); free_itpType(pret1); DBG_INTERPRET(pret); return pret; break; case ';': pret1 = interpret(p->opr.op[0]); pret2 = interpret(p->opr.op[1]); if (pret1) free_itpType(pret1); if (pret2) free_itpType(pret2); DBG_INTERPRET(pret); return pret; break; case '=': pret2 = interpret(p->opr.op[1]); if (pret2 == NULL) return NULL; if (pret2->type == typeLong) symtab[p->opr.op[0]->var.index].ival = pret2->ival; else if (pret2->type == typeStr) { if (symtab[p->opr.op[0]->var.index].type == typeStr) free(symtab[p->opr.op[0]->var.index].pstr); symtab[p->opr.op[0]->var.index].pstr = pret2->pstr; } symtab[p->opr.op[0]->var.index].type = pret2->type; free(pret2); /* do not free pret2->pstr, as it is used by the variable */ DBG_INTERPRET(pret); return pret; break; case UMINUS: pret1 = interpret(p->opr.op[0]); if (pret1 == NULL) return NULL; if (pret1->type == typeLong) { pret->type = typeLong; pret->ival = -pret1->ival; } else { free_itpType(pret1); free_itpType(pret); return NULL; } free_itpType(pret1); DBG_INTERPRET(pret); return pret; break; case PP: if (symtab[p->opr.op[0]->var.index].type == typeLong) { symtab[p->opr.op[0]->var.index].ival += 1; pret->type = typeLong; pret->ival = symtab[p->opr.op[0]->var.index].ival; } else { free_itpType(pret); return NULL; } DBG_INTERPRET(pret); return pret; break; case SS: if (symtab[p->opr.op[0]->var.index].type == typeLong) { symtab[p->opr.op[0]->var.index].ival -= 1; pret->type = typeLong; pret->ival = symtab[p->opr.op[0]->var.index].ival; } else { free_itpType(pret); return NULL; } DBG_INTERPRET(pret); return pret; break; case '+': pret1 = interpret(p->opr.op[0]); pret2 = interpret(p->opr.op[1]); if (pret1 == NULL || pret2 == NULL) { if (pret1 == NULL) free_itpType(pret2); if (pret2 == NULL) free_itpType(pret1); free_itpType(pret); return NULL; } if (pret1->type == typeLong && pret2->type == typeLong) { pret->type = typeLong; pret->ival = pret1->ival + pret2->ival; free_itpType(pret1); free_itpType(pret2); DBG_INTERPRET(pret); return pret; } else if (pret1->type == typeStr && pret2->type == typeStr) { int m = strlen(pret1->pstr); int n = strlen(pret2->pstr); char *s = malloc(m+n+1); if (s == NULL) { free_itpType(pret1); free_itpType(pret2); free_itpType(pret); return NULL; } strncpy(s, pret1->pstr, m); s[m] = '\0'; strncat(s, pret2->pstr, n); s[m+n] = '\0'; free_itpType(pret1); free_itpType(pret2); pret->type = typeStr; pret->pstr = s; DBG_INTERPRET(pret); return pret; } else { yyerror("add type not match\n"); free_itpType(pret1); free_itpType(pret2); free_itpType(pret); return NULL; } break; case '-': case '*': case '/': case '<': case '>': case '%': case AND: case OR: case GE: case LE: pret1 = interpret(p->opr.op[0]); pret2 = interpret(p->opr.op[1]); if (pret1 == NULL || pret2 == NULL) { if (pret1 == NULL) free_itpType(pret2); if (pret2 == NULL) free_itpType(pret1); free_itpType(pret); return NULL; } if (pret1->type == typeLong && pret2->type == typeLong) { pret->type = typeLong; if (p->opr.name == '-') pret->ival = pret1->ival - pret2->ival; else if (p->opr.name == '*') pret->ival = pret1->ival * pret2->ival; else if (p->opr.name == '/') pret->ival = pret1->ival / pret2->ival; else if (p->opr.name == '<') pret->ival = pret1->ival < pret2->ival; else if (p->opr.name == '>') pret->ival = pret1->ival > pret2->ival; else if (p->opr.name == '%') pret->ival = pret1->ival % pret2->ival; else if (p->opr.name == AND) pret->ival = pret1->ival && pret2->ival; else if (p->opr.name == OR) pret->ival = pret1->ival || pret2->ival; else if (p->opr.name == GE) pret->ival = pret1->ival >= pret2->ival; else if (p->opr.name == LE) pret->ival = pret1->ival <= pret2->ival; free_itpType(pret1); free_itpType(pret2); DBG_INTERPRET(pret); return pret; } else { yyerror("type not match\n"); free_itpType(pret1); free_itpType(pret2); free_itpType(pret); return NULL; } break; case EQ: case NE: pret1 = interpret(p->opr.op[0]); pret2 = interpret(p->opr.op[1]); if (pret1 == NULL || pret2 == NULL) { if (pret1 == NULL) free_itpType(pret2); if (pret2 == NULL) free_itpType(pret1); free_itpType(pret); return NULL; } if (pret1->type == typeLong && pret2->type == typeLong) { pret->type = typeLong; if (p->opr.name == EQ) pret->ival = pret1->ival == pret2->ival; else if (pret->type == NE) pret->ival = pret1->ival != pret2->ival; free_itpType(pret1); free_itpType(pret2); DBG_INTERPRET(pret); return pret; } else if (pret1->type == typeStr && pret2->type == typeStr) { pret->type == typeLong; if (p->opr.name == EQ) pret->ival = !strcmp(pret1->pstr, pret2->pstr); else if (p->opr.name == NE) { pret->ival = !!strcmp(pret1->pstr, pret2->pstr); } free_itpType(pret1); free_itpType(pret2); DBG_INTERPRET(pret); return pret; } else { yyerror("add type not match\n"); free_itpType(pret1); free_itpType(pret2); free_itpType(pret); return NULL; } break; case CALL: pret1 = interpret(p->opr.op[0]); if (pret1 == NULL) { free_itpType(pret); return NULL; } if (pret1->type != typeStr) { free_itpType(pret1); free_itpType(pret); return NULL; } pret->type = typeStr; pret->pstr = call_func(pret1->pstr); free_itpType(pret1); if (pret->pstr == NULL) { free_itpType(pret); return NULL; } DBG_INTERPRET(pret); return pret; break; case GET: pret1 = interpret(p->opr.op[0]); pret2 = interpret(p->opr.op[1]); pret3 = interpret(p->opr.op[2]); pret4 = interpret(p->opr.op[3]); if (pret1->type == typeStr && pret2->type == typeLong && pret3->type == typeLong && pret4->type == typeLong) { pret->type = typeStr; pret->pstr = get_func(pret1->pstr, pret2->ival, pret3->ival, pret4->ival); if (pret->pstr == NULL) { free_itpType(pret); pret = NULL; } } free_itpType(pret1); free_itpType(pret2); free_itpType(pret3); free_itpType(pret4); DBG_INTERPRET(pret); return pret; break; case LINES: pret1 = interpret(p->opr.op[0]); pret->type = typeLong; if (pret1->type == typeStr) pret->ival = lines_func(pret1->pstr); else pret->ival = 0; free_itpType(pret1); DBG_INTERPRET(pret); return pret; break; case LEN: pret1 = interpret(p->opr.op[0]); pret->type = typeLong; if (pret1->type == typeStr) pret->ival = strlen(pret1->pstr); else pret->ival = 0; free_itpType(pret1); DBG_INTERPRET(pret); return pret; break; case STR2LONG: pret1 = interpret(p->opr.op[0]); pret->type = typeLong; if (pret1->type != typeStr) { yyerror("str2long need a string as param\n"); free_itpType(pret1); free_itpType(pret); return NULL; } pret->ival = str2long(pret1->pstr); free_itpType(pret1); return pret; break; case LONG2DECSTR: case LONG2HEXSTR: pret1 = interpret(p->opr.op[0]); if (pret1->type != typeLong) { yyerror("long2decstr need a number as param\n"); free_itpType(pret1); free_itpType(pret); return NULL; } else { char *s; int len; s = malloc(MAX_CONVERT_STRLEN); if (s == NULL) { yyerror("malloc for long2decstr failed\n"); free_itpType(pret1); free_itpType(pret); return NULL; } if (p->opr.name == LONG2DECSTR) len = snprintf(s, MAX_CONVERT_STRLEN, "%lld", pret1->ival); else len = snprintf(s, MAX_CONVERT_STRLEN, "0x%llx", pret1->ival); s[len] = '\0'; pret->type = typeStr; pret->pstr = s; return pret; } break; default: yyerror("bad operator!\n"); } default: yyerror("bad node type!\n"); } pret->ival = 0; return pret; }
void execute_line(void) { int type; double op2; double tmp, tmp2; extern int sp; char s[MAXOP]; while ((type = getop(s)) != EOF) { switch (type) { case NUMBER: push(atof(s)); break; case '+': push(pop() + pop()); break; case '*': push(pop() * pop()); break; case '-': op2 = pop(); push(pop() - op2); break; case '/': op2 = pop(); if (op2 != 0.0) push(pop() / op2); else printf("error: zero divisor\n"); break; case '%': op2 = pop(); push((int) pop() % (int) op2); break; case '\n': printf("\t\t%.8g\n", assign('$', pop())); return; /* 一行全部処理したのでリターン */ case IDENTIFIER: if (s[1] == '\0') { switch (s[0]) { case 'p': tmp = pop(); printf("\t%.8g\n", tmp); push(tmp); break; case 'c': tmp = pop(); push(tmp); push(tmp); break; case 'x': tmp = pop(); tmp2 = pop(); push(tmp); push(tmp2); break; case 'e': while (sp != 0) pop(); break; default: goto FunctionCall; } goto NextToken; } FunctionCall: call_func(s); break; case VAR_REF: if (reference(s[1], &tmp) == 0) { push(tmp); } else { printf("undefined variable %c\n", s[1]); goto Error; } break; case ASSIGN: assign(s[1], top()); break; case ERROR: Error: { int c; // while ((c = getch()) != '\n' || c != EOF) // ; } break; default: printf("error: unknown command %s\n", s); break; } NextToken:; } }
Boxed_Value call_func(const chaiscript::dispatch::detail::Function_Signature<Ret (Params...)> &sig, const Callable &f, const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions) { return Handle_Return<Ret>::handle(call_func(sig, std::index_sequence_for<Params...>{}, f, params, t_conversions)); }
int main() { call_func(foo); return 0; }