require "luascape.system"

loadScapeLibrary("reflectreflectors")

reflect = {
  typeRegistry = scape.reflect.TypeRegistry.instance()
}

function reflect.makeNamespaceGlobal(namespace)
  for i,type in pairs(scape.reflect.TypeRegistry.getNamespaceTypes(namespace)) do
    if not type.referencedType then
      local function makeGlobal(type)
        rawset(_G, type:getUndecoratedName(), type)
      end
      pcall(makeGlobal, type)
    end
  end
end

function reflect.loadReflectors(name)
  if not scape.reflect.TypeRegistry.containsNamespace(name) then
    local success, err = pcall(loadScapeLibrary, name.."reflectors") 
    if not success then
      print("Warning: could not load reflector library '"..name.."': "..err)
    end
  end
end

reflect.makeNamespaceGlobal("scape::reflect")
reflect.makeNamespaceGlobal("scape::lua")
