Add accRegisterSymbolCallback API to control external symbol linkage.
Until now dlsym was used to lookup external symbols. Now you can
register your own function to be called when an undefined symbol is
used.
diff --git a/libacc/acc.cpp b/libacc/acc.cpp
index 119d106..36f30a1 100644
--- a/libacc/acc.cpp
+++ b/libacc/acc.cpp
@@ -3232,6 +3232,8 @@
char* dptr; // Macro state: Points to macro text during macro playback.
int dch; // Macro state: Saves old value of ch during a macro playback.
char* pGlobalBase;
+ ACCSymbolLookupFn mpSymbolLookupFn;
+ void* mpSymbolLookupContext;
// Arena for the duration of the compile
Arena mGlobalArena;
@@ -3792,6 +3794,7 @@
}
return false;
}
+
/* Parse and evaluate a unary expression.
* allowAssignment is true if '=' parsing wanted (quick hack)
*/
@@ -3880,9 +3883,12 @@
}
VariableInfo* pVI = VI(t);
n = (intptr_t) pVI->pAddress;
- /* forward reference: try dlsym */
+ /* forward reference: try our lookup function */
if (!n) {
- n = (intptr_t) dlsym(RTLD_DEFAULT, nameof(t));
+ if (mpSymbolLookupFn) {
+ n = (intptr_t) mpSymbolLookupFn(
+ mpSymbolLookupContext, nameof(t));
+ }
if (pVI->pType == NULL) {
if (tok == '(') {
pVI->pType = mkpIntFn;
@@ -4613,6 +4619,12 @@
}
}
+ // One-time initialization, when class is constructed.
+ void init() {
+ mpSymbolLookupFn = 0;
+ mpSymbolLookupContext = 0;
+ }
+
void clear() {
tok = 0;
tokc = 0;
@@ -4673,6 +4685,7 @@
};
Compiler() {
+ init();
clear();
}
@@ -4680,6 +4693,11 @@
cleanup();
}
+ void registerSymbolCallback(ACCSymbolLookupFn pFn, ACCvoid* pContext) {
+ mpSymbolLookupFn = pFn;
+ mpSymbolLookupContext = pContext;
+ }
+
int compile(const char* text, size_t textLength) {
int result;
@@ -4848,6 +4866,10 @@
delete text;
}
+ void registerSymbolCallback(ACCSymbolLookupFn pFn, ACCvoid* pContext) {
+ compiler.registerSymbolCallback(pFn, pContext);
+ }
+
void setError(ACCenum error) {
if (accError == ACC_NO_ERROR && error != ACC_NO_ERROR) {
accError = error;
@@ -4883,6 +4905,12 @@
}
extern "C"
+void accRegisterSymbolCallback(ACCscript* script, ACCSymbolLookupFn pFn,
+ ACCvoid* pContext) {
+ script->registerSymbolCallback(pFn, pContext);
+}
+
+extern "C"
void accScriptSource(ACCscript* script,
ACCsizei count,
const ACCchar ** string,