ejsval Module_prototype_getOrInsertFunction(ejsval env, ejsval _this, int argc, ejsval *args) { Module *module = ((Module*)EJSVAL_TO_OBJECT(_this)); REQ_UTF8_ARG(0, name); REQ_LLVM_TYPE_ARG(1, returnType); REQ_ARRAY_ARG(2, paramTypes); std::vector< llvm::Type*> param_types; for (int i = 0; i < EJSARRAY_LEN(paramTypes); i ++) { param_types.push_back (Type_GetLLVMObj(EJSDENSEARRAY_ELEMENTS(paramTypes)[i])); } llvm::FunctionType *FT = llvm::FunctionType::get(returnType, param_types, false); llvm::Function* f = static_cast< llvm::Function*>(module->llvm_module->getOrInsertFunction(name, FT)); // XXX this needs to come from the js call, since when we hoist anonymous methods we'll need to give them a private linkage. f->setLinkage (llvm::Function::ExternalLinkage); // XXX the args might not be identifiers but might instead be destructuring expressions. punt for now. #if notyet // Set names for all arguments. unsigned Idx = 0; for (Function::arg_iterator AI = F->arg_begin(); Idx != Args.size(); ++AI, ++Idx) AI->setName(Args[Idx]); #endif return Function_new (f); }
ejsval StructType_create(ejsval env, ejsval _this, int argc, ejsval *args) { REQ_UTF8_ARG (0, name); REQ_ARRAY_ARG (1, elementTypes); std::vector<llvm::Type*> element_types; for (int i = 0; i < EJSARRAY_LEN(elementTypes); i ++) { element_types.push_back (Type_GetLLVMObj(EJSDENSEARRAY_ELEMENTS(elementTypes)[i])); } ejsval rv = StructType_new(llvm::StructType::create(llvm::getGlobalContext(), element_types, name)); free (name); return rv; }
static ejsval FunctionType_get (ejsval env, ejsval _this, int argc, ejsval *args) { REQ_LLVM_TYPE_ARG(0, returnType); REQ_ARRAY_ARG(1, argTypes); std::vector<llvm::Type*> arg_types; for (int i = 0; i < EJSARRAY_LEN(argTypes); i ++) { arg_types.push_back (Type_GetLLVMObj(EJSDENSEARRAY_ELEMENTS(argTypes)[i])); } llvm::FunctionType *FT = llvm::FunctionType::get(returnType, arg_types, false); EJSObject* result = FunctionType_alloc_instance(); _ejs_init_object (result, _ejs_FunctionType_proto, NULL); ((FunctionType*)result)->type = FT; return OBJECT_TO_EJSVAL(result); }
ejsval Module_prototype_getOrInsertExternalFunction(ejsval env, ejsval _this, int argc, ejsval *args) { Module *module = ((Module*)EJSVAL_TO_OBJECT(_this)); REQ_UTF8_ARG(0, name); REQ_LLVM_TYPE_ARG(1, returnType); REQ_ARRAY_ARG(2, paramTypes); std::vector< llvm::Type*> param_types; for (int i = 0; i < EJSARRAY_LEN(paramTypes); i ++) { param_types.push_back (Type_GetLLVMObj(EJSDENSEARRAY_ELEMENTS(paramTypes)[i])); } llvm::FunctionType *FT = llvm::FunctionType::get(returnType, param_types, false); llvm::Function* f = static_cast< llvm::Function*>(module->llvm_module->getOrInsertFunction(name, FT)); f->setLinkage (llvm::Function::ExternalLinkage); return Function_new (f); }