Jason McKesson avatar Jason McKesson committed ab57c4d

Source file generation partially done.
Fixed a major bug in header generation.

Comments (0)

Files changed (12)

 	return spec.FuncNamePrefix() .. func.name
 end
 
+function common.GetProcAddressName(spec)
+	return "IntGetProcAddress"
+end
+
 
 return common

StylePointerC.lua

 	hFile:write("/*", headingName, "*/\n")
 end
 
+----------------------------------------------------------------
+-- Header file construction
+
 function my_style.header.CreateFile(basename, options)
 	local filename = basename .. ".h"
 	return common.CreateFile(filename, options.indent), filename
 	hFile:write("extern int ", GetExtVariableName(ext, spec, options), ";\n");
 end
 
-function my_style.header.WriteBeginEnumDeclBlock(hFile, specData, options)
-end
+function my_style.header.WriteBeginEnumDeclBlock(hFile, specData, options) end
 
-function my_style.header.WriteEndEnumDeclBlock(hFile, specData, options)
-end
+function my_style.header.WriteEndEnumDeclBlock(hFile, specData, options) end
 
 function my_style.header.WriteEnumDecl(hFile, enum, enumTable, spec, options)
 	hFile:fmt("#define %s%s = %s\n",
 end
 
 local function GetFuncPtrDef(hFile, func, typemap, spec, options)
-	return string.format("%s (*%s)(%s)",
+	return string.format("%s (%s *%s)(%s)",
 		common.GetFuncReturnType(func, typemap),
+		spec.GetCodegenPtrType(),
 		GetFuncPtrName(func, spec, options),
 		common.GetFuncParamList(func, typemap))
 end
 		DecorateFuncName("IsVersionGEQ", spec, options))
 end
 
-
 function my_style.header.WriteEndDecl(hFile, specData, options)
 	common.WriteExternCEnd(hFile)
 end
 
+--------------------------------------------------
+-- Source file construction functions.
+
+function my_style.source.CreateFile(basename, options)
+	local filename = basename .. ".c"
+	return common.CreateFile(filename, options.indent), filename
+end
+
+function my_style.source.WriteIncludes(hFile, spec, options)
+	hFile:write("#include <stdlib.h>\n")
+	hFile:write("#include <string.h>\n")
+end
+
+local function GetMapTableStructName(spec, options)
+	return string.format("%s%sStrToExtMap", options.prefix, spec.DeclPrefix())
+end
+
+function my_style.source.WriteMapTableDefs(hFile, spec, options)
+	hFile:write("typedef int (*PFN_LOADEXTENSION)()\n")
+	hFile:fmt("typedef struct %s%sStrToExtMap_s\n",
+		options.prefix, spec.DeclPrefix())
+	hFile:write("{\n")
+	hFile:inc()
+	hFile:write("char *strExtensionName;\n")
+	hFile:write("int *pExtensionVariable;\n")
+	hFile:write("PFN_LOADEXTENSION LoadExtension;\n")
+	hFile:dec()
+	hFile:fmt("} %s\n", GetMapTableStructName(spec, options))
+end
+
+function my_style.source.WriteBeginDef(hFile, spec, options) end
+function my_style.source.WriteEndDef(hFile, spec, options) end
+
+function my_style.source.WriteExtVariableDef(hFile, ext, specData, spec, options)
+	hFile:fmt("int %s = %s;\n", GetExtVariableName(ext, spec, options),
+		GetStatusCodeName("LOAD_FAILED", spec, options));
+end
+
+function my_style.source.WriteBeginExtFuncDefBlock(hFile, extName, spec, options)
+end
+
+function my_style.source.WriteEndExtFuncDefBlock(hFile, extName, spec, options)
+end
+
+function my_style.source.WriteFuncDef(hFile, func, typemap, spec, options)
+	--Declare the function pointer.
+	hFile:fmt("%s = NULL;\n",
+		GetFuncPtrDef(hFile, func, typemap, spec, options))
+end
+
+local function GetExtLoaderFuncName(extName, spec, options)
+	return "Load_" .. extName;
+end
+
+function my_style.source.WriteBeginExtLoaderBlock(hFile, extName, spec, options)
+	hFile:fmt("static int %s()\n", GetExtLoaderFuncName(extName, spec, options))
+	hFile:write("{\n")
+	hFile:inc()
+	hFile:write("int numFailed = 0;\n")
+end
+
+function my_style.source.WriteEndExtLoaderBlock(hFile, extName, spec, options)
+	hFile:write("return numFailed;\n")
+	hFile:dec()
+	hFile:write("}\n")
+end
+
+function my_style.source.WriteExtFuncLoader(hFile, func, typemap, spec, options)
+	hFile:fmt('%s = %s("%s%s");\n',
+		GetFuncPtrName(func, spec, options),
+		common.GetProcAddressName(spec),
+		spec.FuncNamePrefix(), func.name)
+	hFile:fmt('if(!%s) numFailed++;\n', GetFuncPtrName(func, spec, options))
+end
+
+function my_style.source.WriteBeginCoreFuncDefBlock(hFile, version, spec, options)
+end
+
+function my_style.source.WriteEndCoreFuncDefBlock(hFile, version, spec, options)
+end
+
+local function GetCoreLoaderFuncName(version, spec, options)
+	return "Load_Version_" .. version:gsub("%.", "_")
+end
+
+function my_style.source.WriteBeginCoreLoaderBlock(hFile, version, spec, options)
+	hFile:fmt("static int %s()\n", GetCoreLoaderFuncName(version, spec, options))
+	hFile:write("{\n")
+	hFile:inc()
+	hFile:write("int numFailed = 0;\n")
+end
+
+function my_style.source.WriteEndCoreLoaderBlock(hFile, version, spec, options)
+	hFile:write("return numFailed;\n")
+	hFile:dec()
+	hFile:write("}\n")
+end
+
+function my_style.source.WriteCoreFuncLoader(hFile, func, typemap, spec, options)
+	hFile:fmt('%s = %s("%s%s");\n',
+		GetFuncPtrName(func, spec, options),
+		common.GetProcAddressName(spec),
+		spec.FuncNamePrefix(), func.name)
+
+	--Special hack for DSA_EXT functions in core functions.
+	--They do not count against the loaded count.
+	if(func.name:match("EXT$")) then
+		hFile:write("/*An EXT_direct_state_access-based function. Don't count it.*/")
+	else
+		hFile:fmt('if(!%s) numFailed++;\n', GetFuncPtrName(func, spec, options))
+	end
+end
+
+function my_style.source.WriteUtilityDefs(hFile, specData, spec, options)
+	hFile:write("Utilities\n")
+end
 
 --------------------------------------------------
 -- Style retrieval machinery
 		end
 	end
 	
-	hFile:write("\n")
-	
 	--For each version we are told to export, write the Functions.
 	if(options.version) then
+		hFile:write("\n")
 		style.WriteSmallHeading(hFile, "Core Functions")
+	else
+		--No version to export
+		return
 	end
 	
 	local coreExts = spec.GetCoreExts()
 	hFile:rawwrite(spec.GetHeaderInit())
 	
 	--Write the standard typedefs.
-	header.WriteStdTypedefs(hFile, specData, options)
+	header.WriteStdTypedefs(hFile, specData, spec, options)
 	
 	--Write the typedefs from the spec.
-	header.WriteSpecTypedefs(hFile, specData, options)
+	header.WriteSpecTypedefs(hFile, specData, spec, options)
 	
 	--Write any declaration scoping start.
-	header.WriteBeginDecl(hFile, specData, options)
+	header.WriteBeginDecl(hFile, spec, options)
 	
 	--Write the extension variable declarations.
 	style.WriteLargeHeading(hFile, "Extension variable declarations")
 	
 	--Write all enumerators.
 	style.WriteLargeHeading(hFile, "Enumerators")
-	header.WriteBeginEnumDeclBlock(hFile, specData, options)
+	header.WriteBeginEnumDeclBlock(hFile, spec, options)
 
 	WriteEnumerators(hFile, options, spec, style, specData)
 	
-	header.WriteEndEnumDeclBlock(hFile, specData, options)
+	header.WriteEndEnumDeclBlock(hFile, spec, options)
 	
 	--Write all function declarations
 	style.WriteLargeHeading(hFile, "Functions")
-	header.WriteBeginFuncDeclBlock(hFile, specData, options)
+	header.WriteBeginFuncDeclBlock(hFile, spec, options)
 
 	WriteFunctionDecls(hFile, options, spec, style, specData)
 	
-	header.WriteEndFuncDeclBlock(hFile, specData, options)
+	header.WriteEndFuncDeclBlock(hFile, spec, options)
 	
 	--Write the function loading stuff.
 	style.WriteLargeHeading(hFile, "Loading Functions")
 	end
 	
 	--Write any declaration scoping end.
-	header.WriteEndDecl(hFile, specData, options)
+	header.WriteEndDecl(hFile, spec, options)
 	
 	--Ending includeguard.
 	hFile:fmt("#endif //%s\n", inclGuard)
 	return filename
 end
 
+
+
+local function WriteFuncDefsFromList(hFile, funcList, funcSeen,
+	listName, options, spec, style, specData)
+	local source = style.source
+	
+	local loaded = {}
+	for _, func in ipairs(funcList) do
+		if(not funcSeen[func.name]) then
+			source.WriteFuncDef(hFile, func, specData.typemap, spec, options)
+			funcSeen[func.name] = listName
+			loaded[#loaded + 1] = func
+		end
+	end
+	
+	return loaded
+end
+
+local function WriteExtFuncLoaderFromList(hFile, funcList,
+	options, spec, style, specData)
+	local source = style.source
+	
+	for _, func in ipairs(funcList) do
+		source.WriteExtFuncLoader(hFile, func, specData.typemap, spec, options)
+	end
+end
+
+local function WriteCoreFuncLoaderFromList(hFile, funcList,
+	options, spec, style, specData)
+	local source = style.source
+	
+	for _, func in ipairs(funcList) do
+		source.WriteCoreFuncLoader(hFile, func, specData.typemap, spec, options)
+	end
+end
+
+local function WriteFuncDefsForExt(hFile, extName, funcSeen, options, spec,
+	style, specData)
+	local source = style.source
+
+	if(#specData.extdefs[extName].funcs > 0) then
+		style.WriteSmallHeading(hFile, spec.ExtNamePrefix() .. extName)
+		
+		source.WriteBeginExtFuncDefBlock(hFile, extName, spec, options)
+		local loaded = WriteFuncDefsFromList(hFile,
+			specData.extdefs[extName].funcs, funcSeen, extName,
+			options, spec, style, specData)
+		
+		hFile:write("\n")
+		source.WriteBeginExtLoaderBlock(hFile, extName, spec, options)
+		WriteExtFuncLoaderFromList(hFile, loaded, options,
+			spec, style, specData)
+		source.WriteEndExtLoaderBlock(hFile, extName, spec, options)
+		
+		source.WriteEndExtFuncDefBlock(hFile, extName, spec, options)
+		hFile:write("\n")
+	end
+end
+
+local function WriteFuncDefsForCoreExt(hFile, extName, funcSeen, options, spec,
+	style, specData)
+	local source = style.source
+
+	if(#specData.extdefs[extName].funcs > 0) then
+		style.WriteSmallHeading(hFile, spec.ExtNamePrefix() .. extName)
+		
+		local loaded = WriteFuncDefsFromList(hFile,
+			specData.extdefs[extName].funcs, funcSeen, extName,
+			options, spec, style, specData)
+		hFile:write("\n")
+	end
+end
+
+local function WriteFunctionDefs(hFile, options, spec, style, specData)
+	local source = style.source
+	
+	style.WriteSmallHeading(hFile, "Extension Functions")
+
+	local extSeen = {}
+	local funcSeen = {}
+	
+	--For each extension, write their function pointer definitions.
+	for _, extName in ipairs(options.extensions) do
+		if(not extSeen[extName]) then
+			extSeen[extName] = true
+			WriteFuncDefsForExt(hFile, extName, funcSeen, options,
+				spec, style, specData)
+		end
+	end
+	
+	--For each version we are told to export, write the function pointer definitions.
+	if(options.version) then
+		hFile:write("\n")
+		style.WriteSmallHeading(hFile, "Core Functions")
+	else
+		--No version to export, so don't bother.
+		return
+	end
+	
+	--Write the core function definitions, maintaining a list of everything
+	--that was written.
+	local coreExts = spec.GetCoreExts()
+	local writtenData = { coreexts = {}, corefuncs = {}}
+	local bWrittenBeginCore = false
+	for _, version in ipairs(spec.GetVersions()) do
+		if(tonumber(version) <= tonumber(options.version)) then
+			--Write any core extensions for that version.
+			if(coreExts[version]) then
+				for _, extName in ipairs(coreExts[version]) do
+					if(not extSeen[extName]) then
+						if(not bWrittenBeginCore) then
+							source.WriteBeginCoreFuncDefBlock(hFile, options.version, spec, options)
+							bWrittenBeginCore = true
+						end
+						extSeen[extName] = true
+						WriteFuncDefsForCoreExt(hFile, extName, funcSeen,
+							options, spec, style, specData)
+						table.insert(writtenData.coreexts, extName)
+					end
+				end
+			end
+			
+			--Write the actual core functions, if any.
+			local funcList = GetCoreFunctions(specData.coredefs[version],
+				specData, spec, options, version)
+				
+			if(#funcList > 0) then
+				if(not bWrittenBeginCore) then
+					source.WriteBeginCoreFuncDefBlock(hFile, options.version, spec, options)
+					bWrittenBeginCore = true
+				end
+				style.WriteSmallHeading(hFile, "Version " .. version)
+				
+				WriteFuncDefsFromList(hFile, funcList, funcSeen,
+					version, options, spec, style, specData)
+
+				hFile:write("\n")
+				table.insert(writtenData.corefuncs, funcList)
+			end
+		end
+	end
+	
+	--Now, write the function that loads the core version. Include
+	--ALL core extensions, not just the ones we wrote.
+	--This allows us to build an accurate count of what core stuff is missing.
+	if(bWrittenBeginCore) then
+		source.WriteBeginCoreLoaderBlock(hFile, options.version, spec, options)
+		for _, version in ipairs(spec.GetVersions()) do
+			if(tonumber(version) <= tonumber(options.version)) then
+				if(coreExts[version]) then
+					for _, extName in ipairs(coreExts[version]) do
+						WriteCoreFuncLoaderFromList(hFile,
+							specData.extdefs[extName].funcs,
+							options, spec, style, specData)
+					end
+				end
+			end
+
+			--Write the actual core functions, if any.
+			local funcList = GetCoreFunctions(specData.coredefs[version],
+				specData, spec, options, version)
+				
+			if(#funcList > 0) then
+				WriteCoreFuncLoaderFromList(hFile,
+					funcList, options, spec, style, specData)
+			end
+		end
+
+		source.WriteEndCoreLoaderBlock(hFile, options.version, spec, options)
+
+		source.WriteEndCoreFuncDefBlock(hFile, options.version, spec, options)
+	end
+end
+
+local function BuildSource(options, spec, style, specData, basename,
+	hdrFilename)
+	local source = style.source
+	local hFile, filename = source.CreateFile(basename, options)
+	
+	--Write the header inclusions
+	source.WriteIncludes(hFile, spec, options)
+	hFile:fmt('#include "%s"\n', hdrFilename:match("([^\\/]+)$"))
+	hFile:write("\n")
+
+	--Write the function that loads a function pointer, given a name.
+	hFile:writeblock(spec.GetLoaderFunc())
+	hFile:write("\n")
+	
+	--Write any definitions scoping start.
+	source.WriteBeginDef(hFile, spec, options)
+	
+	--Write the extension variable definitions.
+	style.WriteLargeHeading(hFile, "Extension variable definitions")
+	for _, ext in ipairs(options.extensions) do
+		source.WriteExtVariableDef(hFile, ext, specData, spec, options)
+	end
+	hFile:write("\n")
+	
+	--Write all of the loader definitions.
+	style.WriteLargeHeading(hFile, "Function Definitions and Loaders")
+	WriteFunctionDefs(hFile, options, spec, style, specData)
+	hFile:write("\n")
+	
+	--Write utility definitions needed by the loader.
+	source.WriteUtilityDefs(hFile, specData, spec, options)
+
+	--Write any definitions scoping end.
+	source.WriteEndDef(hFile, spec, options)
+
+	return filename
+end
+
 local function Generate(options, specData)
 	--Extract the path and base-filename from the options.
 	local simplename = options.outname:match("([^\\/]+)$")
 	--Compute the filename, minus style-specific suffix.
 	local basename = dir .. spec:FilePrefix() .. simplename
 	
-	BuildHeader(options, spec, style, specData, basename)
+	local hdrFilename = BuildHeader(options, spec, style, specData, basename)
+	local srcFilename = BuildSource(options, spec, style, specData, basename,
+		hdrFilename)
 end
 
 local function LoadSpec(options)
 function wgl_spec.GetLoaderParams() return "HDC *hdc" end
 function glx_spec.GetLoaderParams() return "Display *display, int screen" end
 
+--CodeGen function pointer type. For APIFUNC and so forth.
+function gl_spec.GetCodegenPtrType() return "CODEGEN_FUNCPTR" end
+function wgl_spec.GetCodegenPtrType() return "CODEGEN_FUNCPTR" end
+function glx_spec.GetCodegenPtrType() return "CODEGEN_FUNCPTR" end
+
+--Name of the function that loads pointers
+function gl_spec.GetPtrLoaderFuncName() return "IntGetProcAddress" end
+function wgl_spec.GetPtrLoaderFuncName() return "IntGetProcAddress" end
+function glx_spec.GetPtrLoaderFuncName() return "IntGetProcAddress" end
+
 local fileProps =
 {
 	{"GetHeaderInit", "init"},
 	{"GetVersions", "versions"},
 	{"GetCoreExts", "coreexts"},
+	{"GetLoaderFunc", "loaderfunc"},
 }
 
 --Header initialization.
 	end
 end
 
---Get version numbers
-
 
 --------------------------------------------------
 -- Spec retrieval machinery
 - header.MakeIncludeGuard(prefix, specIncl)
 --		specIncl is an include-guard string from the spec. It returns a string that includes the two strings, which is appropriate for use as an include-guard.
 
-- header.WriteStdTypedefs(hFile, specData, options)
+- header.WriteStdTypedefs(hFile, specData, spec, options)
 --		Writes the standard typedefs.
 
-- header.WriteSpecTypedefs(hFile, specData, options)
+- header.WriteSpecTypedefs(hFile, specData, spec, options)
 --		Writes the typedefs from the spec. The "funcData.passthru" section.
 
-- header.WriteBeginDecl(hFile, specData, options)
+- header.WriteBeginDecl(hFile, spec, options)
 --		Writes any style-specific scoping stuff that begins the declaration section. This is useful for things like 'extern "C"' and so forth.
 --	
 
-- header.WriteEndDecl(hFile, specData, options)
+- header.WriteEndDecl(hFile, spec, options)
 --		Writes any style-specific scoping stuff that ends the declaration section. This is useful for things like 'extern "C"' and so forth.
 
 - header.WriteExtVariableDecl(hFile, ext, specData, spec, options)
 --		Writes the variable declaration for a single extension variable. These are the variables that are exposed so that the user can test to see if an extension loaded.
 
-- header.WriteBeginEnumDeclBlock(hFile, specData, options)
+- header.WriteBeginEnumDeclBlock(hFile, spec, options)
 --		Writes any style-specific scoping stuff that starts the enumerator block. This is for *all* enumerators. This is useful for wrapping all enumerators in an enum declaration.
 
-- header.WriteEndEnumDeclBlock(hFile, specData, options)
+- header.WriteEndEnumDeclBlock(hFile, spec, options)
 --		Writes any style-specific scoping stuff that ends the enumerator block.
 
 - header.WriteEnumDecl(hFile, enum, enumTable, spec, options)
 - header.WriteEnumPrevDecl(hFile, enum, enumTable, spec, options, extName)
 --		Writes an enumerator that was written previously. Should be written as a comment, as it could conflict. extName is the name of the extension where it was originally written.
 
-- header.WriteBeginFuncDeclBlock(hFile, specData, options)
+- header.WriteBeginFuncDeclBlock(hFile, spec, options)
 --	Write any style-specific scoping stuff that starts the function declaration block.
 
-- header.WriteEndFuncDeclBlock(hFile, specData, options)
+- header.WriteEndFuncDeclBlock(hFile, spec, options)
 --	Write any style-specific scoping stuff that ends the function declaration block.
 
 - header.WriteBeginExtFuncDeclBlock(hFile, extName, spec, options)
 --	Writes the declarations for any functions used to help the user get versioning info. Will only be called if the spec has versions.
 
 
+- source.CreateFile(basename, options)
+--		basename is the filename sans extension. It opens a TabbedFile from it, using the options in options. It returns two values: a TabbedFile, and the actual filename used.
+
+- source.WriteIncludes(hFile, spec, options)
+--	Writes the include files for standard library stuff.
+
+- source.WriteBeginDef(hFile, spec, options)
+--	Writes any scoping for the block of definitions.
+
+- source.WriteEndDef(hFile, spec, options)
+--	Ends any scoping for the block of definitions.
+
+- source.WriteExtVariableDef(hFile, ext, specData, spec, options)
+--	Writes the extension variable definition for `ext`.
+
+- source.WriteBeginExtFuncDefBlock(hFile, extName, spec, options)
+--	Writes the beginning of an extension's block that contains function pointer definitions and the loader function for this extension.
+
+- source.WriteEndExtFuncDefBlock(hFile, extName, spec, options)
+--	Writes the ending of an extension's block that contains function pointer definitions and the loader function for this extension.
+
+- source.WriteFuncDef(hFile, func, typemap, spec, options)
+--	Writes a function pointer definition for the given function.
+
+- source.WriteBeginExtLoaderBlock(hFile, extName, spec, options)
+--	Writes the beginning of an extension loader function.
+
+- source.WriteEndExtLoaderBlock(hFile, extName, spec, options)
+--	Writes the ending of an extension loader function.
+
+- source.WriteExtFuncLoader(hFile, func, typemap, spec, options)
+--	Writes the code that goes inside an extension loader function that loads and tests a single extension function pointer.
+
+- source.WriteBeginCoreFuncDefBlock(hFile, version, spec, options)
+--	Writes the beginning of the core loader's block that contains function pointer definitions and the loader function for `version`.
+
+- source.WriteEndCoreFuncDefBlock(hFile, version, spec, options)
+--	Writes the end of the core loader's block that contains function pointer definitions and the loader function for `version`.
+
+- source.WriteBeginCoreLoaderBlock(hFile, version, spec, options)
+--	Writes the start of the actual function that loads the function pointers for `version`.
+
+- source.WriteEndCoreLoaderBlock(hFile, version, spec, options)
+--	Writes the end of the actual function that loads the function pointers for `version`.
+
+- source.WriteCoreFuncLoader(hFile, func, typemap, spec, options)
+--	Writes the code that goes inside the core loader function that loads and tests a single extension function pointer.
+
+- source.WriteUtilityDefs(hFile, specData, spec, options)
+--	A place for writing utilities that will be needed by the function loader code. This can be a mapping table for mapping from extension name to loader funcs and extension variables or whatever you need to do the loading.
+
 If you want to extend this to new styles, then add a module for your style, import it, and register it's module table with the registry. Your module should export a function `Create` which takes no parameters and returns a table as defined above.
 ]]
 
 - pop: Restores the previously preserved tab count.
 - fmt: As string.format followed by an indented write
 - write: An indented write; everything is written after the indent.
+- writeblock: Takes a single string, breaks it down into multiple lines, and writes each line indented.
 - rawfmt: As string.format followed by a NON-indented write.
 - rawwrite: hFile:write.
 
 	rawget(self, "_hFile"):write(...)
 end
 
+function members:writeblock(block, ...)
+	assert(#{...} == 0, "writeblock takes one argument")
+	for line in block:gmatch("([^\n]+)\n") do
+		self:write(line, "\n")
+	end
+	
+	local last = block:match("\n([^\n]*)$")
+	if(#last ~= 0) then
+--		self:write(last)
+	end
+end
+
 function members:rawwrite(...)
 	rawget(self, "_hFile"):write(...)
 end

data/gl_specinit.lua

 	#endif
 #endif /*APIENTRY*/
 
-#ifndef GLE_FUNCPTR
-	#define GLE_REMOVE_FUNCPTR
+#ifndef CODEGEN_FUNCPTR
+	#define CODEGEN_REMOVE_FUNCPTR
 	#if defined(_WIN32)
-		#define GLE_FUNCPTR APIENTRY
+		#define CODEGEN_FUNCPTR APIENTRY
 	#else
-		#define GLE_FUNCPTR
+		#define CODEGEN_FUNCPTR
 	#endif
-#endif /*GLE_FUNCPTR*/
+#endif /*CODEGEN_FUNCPTR*/
 
 #ifndef GLAPI
 	#define GLAPI extern

data/gl_specloaderfunc.lua

+--[[ The function pointer loading function takes a string and returns either NULL or a valid pointer. It is the responsibility of the loader to take care of any platform-specific oddities in pointer fetching.
+]]
+
+return [====[
+#if defined(__APPLE__)
+#include <mach-o/dyld.h>
+
+static void* AppleGLGetProcAddress (const GLubyte *name)
+{
+  static const struct mach_header* image = NULL;
+  NSSymbol symbol;
+  char* symbolName;
+  if (NULL == image)
+  {
+    image = NSAddImage("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL", NSADDIMAGE_OPTION_RETURN_ON_ERROR);
+  }
+  /* prepend a '_' for the Unix C symbol mangling convention */
+  symbolName = malloc(strlen((const char*)name) + 2);
+  strcpy(symbolName+1, (const char*)name);
+  symbolName[0] = '_';
+  symbol = NULL;
+  /* if (NSIsSymbolNameDefined(symbolName))
+	 symbol = NSLookupAndBindSymbol(symbolName); */
+  symbol = image ? NSLookupSymbolInImage(image, symbolName, NSLOOKUPSYMBOLINIMAGE_OPTION_BIND | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR) : NULL;
+  free(symbolName);
+  return symbol ? NSAddressOfSymbol(symbol) : NULL;
+}
+#endif /* __APPLE__ */
+
+#if defined(__sgi) || defined (__sun)
+#include <dlfcn.h>
+#include <stdio.h>
+
+static void* SunGetProcAddress (const GLubyte* name)
+{
+  static void* h = NULL;
+  static void* gpa;
+
+  if (h == NULL)
+  {
+    if ((h = dlopen(NULL, RTLD_LAZY | RTLD_LOCAL)) == NULL) return NULL;
+    gpa = dlsym(h, "glXGetProcAddress");
+  }
+
+  if (gpa != NULL)
+    return ((void*(*)(const GLubyte*))gpa)(name);
+  else
+    return dlsym(h, (const char*)name);
+}
+#endif /* __sgi || __sun */
+
+#if defined(_WIN32)
+
+#pragma warning(disable: 4055)
+#pragma warning(disable: 4054)
+
+static int TestPointer(const void *pTest)
+{
+	ptrdiff_t iTest;
+	if(!pTest) return 0;
+	iTest = (ptrdiff_t)pTest;
+	
+	if(iTest == 1 || iTest == 2 || iTest == 3 || iTest == -1) return 0;
+	
+	return 1;
+}
+
+static void *WinGetProcAddress(const char *name)
+{
+	HMODULE glMod = NULL;
+	void *pFunc = (void*)wglGetProcAddress((LPCSTR)name);
+	if(TestPointer(pFunc))
+	{
+		return pFunc;
+	}
+	glMod = GetModuleHandleA("OpenGL32.dll");
+	return (void*)GetProcAddress(glMod, (LPCSTR)name);
+}
+	
+#define IntGetProcAddress(name) WinGetProcAddress(name)
+#else
+	#if defined(__APPLE__)
+		#define IntGetProcAddress(name) AppleGLGetProcAddress(name)
+	#else
+		#if defined(__sgi) || defined(__sun)
+			#define IntGetProcAddress(name) SunGetProcAddress(name)
+		#else /* GLX */
+		    #include <GL/glx.h>
+
+			#define IntGetProcAddress(name) (*glXGetProcAddressARB)(name)
+		#endif
+	#endif
+#endif
+]====]

data/glx_specinit.lua

 #include <X11/Xlib.h>
 #include <X11/Xutil.h>
 #include <GL/glx.h>
-#define GLE_FUNCPTR
+#ifdef CODEGEN_FUNCPTR
+#undef CODEGEN_FUNCPTR
+#endif /*CODEGEN_FUNCPTR*/
+#define CODEGEN_FUNCPTR
 
 ]]

data/glx_specloaderfunc.lua

+return assert(dofile("data/gl_specloaderfunc.lua"))
+

data/wgl_specinit.lua

 #endif
 #include <windows.h>
 
-#ifdef GLE_FUNCPTR
-#undef GLE_FUNCPTR
-#endif /*GLE_FUNCPTR*/
-#define GLE_FUNCPTR WINAPI
+#ifdef CODEGEN_FUNCPTR
+#undef CODEGEN_FUNCPTR
+#endif /*CODEGEN_FUNCPTR*/
+#define CODEGEN_FUNCPTR WINAPI
 
 ]]

data/wgl_specloaderfunc.lua

+return assert(dofile("data/gl_specloaderfunc.lua"))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.