* import_contract function * restrain name must be pure file name, with no '.lua' extension. And can load bytestream from thinkyoung api. * try load bytestream first, then try write bytes to tmp file and then load the new file */
| 1690 | * try load bytestream first, then try write bytes to tmp file and then load the new file |
| 1691 | */ |
| 1692 | int luaL_import_contract_module(lua_State *L) |
| 1693 | { |
| 1694 | if (lua_gettop(L) < 1 || !lua_isstring(L, 1)) |
| 1695 | { |
| 1696 | global_glua_chain_api->throw_exception(L, THINKYOUNG_API_SIMPLE_ERROR, "import_contract need 1 string argument of contract name"); |
| 1697 | return 0; |
| 1698 | } |
| 1699 | const char *origin_contract_name = luaL_checkstring(L, -1); |
| 1700 | const char *name = origin_contract_name; |
| 1701 | if (glua::util::ends_with(name, ".lua"), glua::util::ends_with(name, GLUA_SOURCE_FILE_EXTENTION_NAME)) { |
| 1702 | global_glua_chain_api->throw_exception(L, THINKYOUNG_API_SIMPLE_ERROR, "this contract byte stream not matched with the info stored in thinkyoung api"); |
| 1703 | return 0; |
| 1704 | } |
| 1705 | bool is_pointer = glua::util::starts_with(name, ADDRESS_CONTRACT_PREFIX); |
| 1706 | bool is_stream = glua::util::starts_with(name, STREAM_CONTRACT_PREFIX); |
| 1707 | std::string name_str; |
| 1708 | if (!is_pointer && !is_stream) |
| 1709 | { |
| 1710 | name_str = thinkyoung::lua::lib::wrap_contract_name(origin_contract_name); |
| 1711 | name = name_str.c_str(); |
| 1712 | } |
| 1713 | auto unwrap_name = thinkyoung::lua::lib::unwrap_any_contract_name(origin_contract_name); |
| 1714 | const char *filename = name; |
| 1715 | lua_settop(L, 1); /* _LOADED table will be at index 2 */ |
| 1716 | lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); |
| 1717 | lua_getfield(L, 2, filename); /* _LOADED[name] */ |
| 1718 | if (lua_toboolean(L, -1)) /* is it there? */ |
| 1719 | return 1; /* package is already loaded */ |
| 1720 | /* else must load package */ |
| 1721 | lua_pop(L, 1); /* remove 'getfield' result */ |
| 1722 | |
| 1723 | size_t global_size_before = luaL_count_global_variables(L); |
| 1724 | std::list<std::string> global_vars_before; |
| 1725 | luaL_get_global_variables(L, &global_vars_before); |
| 1726 | |
| 1727 | // check whether the contract existed |
| 1728 | bool exists; |
| 1729 | std::string namestr(name); |
| 1730 | if (is_pointer) |
| 1731 | { |
| 1732 | std::string address = unwrap_get_contract_address(namestr); |
| 1733 | exists = global_glua_chain_api->check_contract_exist_by_address(L, address.c_str()); |
| 1734 | } |
| 1735 | else if (is_stream) |
| 1736 | { |
| 1737 | GluaModuleByteStream *stream = unwrap_get_contract_stream(namestr); |
| 1738 | exists = true; |
| 1739 | } |
| 1740 | else |
| 1741 | { |
| 1742 | exists = global_glua_chain_api->check_contract_exist(L, origin_contract_name); |
| 1743 | } |
| 1744 | if (!exists) |
| 1745 | { |
| 1746 | global_glua_chain_api->throw_exception(L, THINKYOUNG_API_SIMPLE_ERROR, "this contract not found"); |
| 1747 | return 0; |
| 1748 | } |
| 1749 | if (!is_stream) |
no test coverage detected