## import of pure C/CPP modules with no byte code

def tp_import(TP, const char* name, const char* fname) ->tp_obj:
	tp_obj g = tp_interface_t(tp)
	g.type.magic = TP_MODULE_EXTERNAL_MAGIC
	tp_set(tp, g, tp_string_atom(tp, "__name__"), name)
	tp_set(tp, g, tp_string_atom(tp, "__file__"), fname)
	## WARN cyclic ref can causes debug printing to crash!!!
	## __dict__ is required to support `from x import *`
	tp_set(tp, g, tp_string_atom(tp, "__dict__"), g)
	tp_set(tp, tp->modules, name, g)
	return g

## import of bytecode modules

def tp_import(TP, tp_obj name, tp_obj code, tp_obj fname) ->tp_obj:
	tp_obj g
	g = tp_interface_t(tp)
	tp_set(tp, g, tp_string_atom(tp, "__name__"), name)
	tp_set(tp, g, tp_string_atom(tp, "__file__"), fname)
	//tp_set(tp, g, tp_string_atom(tp, "__code__"), code);
	tp_set(tp, g, tp_string_atom(tp, "__dict__"), g)
	tp_set(tp, tp->modules, name, g)
	##tp_set(tp, tp->globals, name, g)  ## not here
	## an older versoin of the code does not run frame of jmp == 0. Why?
	##tp_enter_frame(tp, globals, code, &r);
	##if (!tp->jmp) {
	##	 tp_run_frame(tp);
	##} 
	if code.type.type_id != TP_NONE:
		tp_exec(tp, code)
	return g

## Function: tp_import
## Imports a module.
## Parameters:
## fname - The filename of a file containing the modules code.
## name - The name of the module.
## codes - The modules code.  If this is given, fname is ignored.
## len - The length of the bytecode.
## Returns:
## The module object.
def tp_import_from_buffer_with_filename(TP, const char * fname, const char * name, void *codes, int len) ->tp_obj:
	#ifdef DEBUG
	  print("tp_import.cpp tp_import_from_buffer:")
	  print(fname)
	  print(name)
	  print(len)
	#endif
	tp_obj f = fname?tp_string_atom(tp, fname):tp_None
	tp_obj bc = codes?tp_string_t_from_const(tp, (const char*)codes, len):tp_None
	return tp_import(tp, tp_string_atom(tp, name), bc, f)


## used by runtime.cpp import corelib
def tp_import_from_buffer(TP, const char * name, unsigned char *codes, int len) ->tp_obj:
	#ifdef DEBUG
	  print("tp_import.cpp tp_import_from_buffer: corelib")
	  std::cout << "	module name: " << name << std::endl;
	  std::cout << "	modules bytes:" << len << std::endl;
	#endif
	tp_obj f = tp_None
	tp_obj bc = tp_string_t_from_const(tp, (const char*)codes, len)
	//return tp_import(tp, tp_string_atom(tp, name), bc, f);
	//std::string s = std::string(  reinterpret_cast< char const* >(codes), len);
	//tp_obj bc = tp_string_from_stdstring(tp, s);  // TODO fix bug in tp_string_from_stdstring
	//if (bc.type.type_id == TP_NONE) {
	//	throw "unable to convert byte code to a tp_string";
	//} else {
	//	std::cout << "load bytecode OK" << std::endl;
	//}
	#if DEBUG > 3
	  std::cout << s << std::endl;
	#endif
	return tp_import(tp, tp_string_atom(tp, name), bc, f)


