<commit_before>#include "binaryninjaapi.h"

using namespace BinaryNinja;
using namespace std;


ScriptingOutputListener::ScriptingOutputListener()
{
	m_callbacks.context = this;
	m_callbacks.output = OutputCallback;
	m_callbacks.error = ErrorCallback;
	m_callbacks.inputReadyStateChanged = InputReadyStateChangedCallback;
}


void ScriptingOutputListener::OutputCallback(void* ctxt, const char* text)
{
	ScriptingOutputListener* listener = (ScriptingOutputListener*)ctxt;
	listener->NotifyOutput(text);
}


void ScriptingOutputListener::ErrorCallback(void* ctxt, const char* text)
{
	ScriptingOutputListener* listener = (ScriptingOutputListener*)ctxt;
	listener->NotifyError(text);
}


void ScriptingOutputListener::InputReadyStateChangedCallback(void* ctxt, BNScriptingProviderInputReadyState state)
{
	ScriptingOutputListener* listener = (ScriptingOutputListener*)ctxt;
	listener->NotifyInputReadyStateChanged(state);
}


void ScriptingOutputListener::NotifyOutput(const string&) {}


void ScriptingOutputListener::NotifyError(const string&) {}


void ScriptingOutputListener::NotifyInputReadyStateChanged(BNScriptingProviderInputReadyState) {}


ScriptingInstance::ScriptingInstance(ScriptingProvider* provider)
{
	BNScriptingInstanceCallbacks cb;
	cb.context = this;
	cb.destroyInstance = DestroyInstanceCallback;
	cb.externalRefTaken = nullptr;
	cb.externalRefReleased = nullptr;
	cb.executeScriptInput = ExecuteScriptInputCallback;
	cb.cancelScriptInput = CancelScriptInputCallback;
	cb.setCurrentBinaryView = SetCurrentBinaryViewCallback;
	cb.setCurrentFunction = SetCurrentFunctionCallback;
	cb.setCurrentBasicBlock = SetCurrentBasicBlockCallback;
	cb.setCurrentAddress = SetCurrentAddressCallback;
	cb.setCurrentSelection = SetCurrentSelectionCallback;
	cb.completeInput = CompleteInputCallback;
	cb.stop = StopCallback;
	AddRefForRegistration();
	m_object = BNInitScriptingInstance(provider->GetObject(), &cb);
}


ScriptingInstance::ScriptingInstance(BNScriptingInstance* instance)
{
	m_object = instance;
}


void ScriptingInstance::DestroyInstanceCallback(void* ctxt)
{
	ScriptingInstance* instance = (ScriptingInstance*)ctxt;
	instance->DestroyInstance();
}


BNScriptingProviderExecuteResult ScriptingInstance::ExecuteScriptInputCallback(void* ctxt, const char* input)
{
	ScriptingInstance* instance = (ScriptingInstance*)ctxt;
	return instance->ExecuteScriptInput(input);
}


void ScriptingInstance::CancelScriptInputCallback(void* ctxt)
{
	ScriptingInstance* instance = (ScriptingInstance*)ctxt;
	instance->CancelScriptInput();
}


void ScriptingInstance::SetCurrentBinaryViewCallback(void* ctxt, BNBinaryView* view)
{
	ScriptingInstance* instance = (ScriptingInstance*)ctxt;
	instance->SetCurrentBinaryView(view ? new BinaryView(BNNewViewReference(view)) : nullptr);
}


void ScriptingInstance::SetCurrentFunctionCallback(void* ctxt, BNFunction* func)
{
	ScriptingInstance* instance = (ScriptingInstance*)ctxt;
	instance->SetCurrentFunction(func ? new Function(BNNewFunctionReference(func)) : nullptr);
}


void ScriptingInstance::SetCurrentBasicBlockCallback(void* ctxt, BNBasicBlock* block)
{
	ScriptingInstance* instance = (ScriptingInstance*)ctxt;
	instance->SetCurrentBasicBlock(block ? new BasicBlock(BNNewBasicBlockReference(block)) : nullptr);
}


void ScriptingInstance::SetCurrentAddressCallback(void* ctxt, uint64_t addr)
{
	ScriptingInstance* instance = (ScriptingInstance*)ctxt;
	instance->SetCurrentAddress(addr);
}


void ScriptingInstance::SetCurrentSelectionCallback(void* ctxt, uint64_t begin, uint64_t end)
{
	ScriptingInstance* instance = (ScriptingInstance*)ctxt;
	instance->SetCurrentSelection(begin, end);
}


char* ScriptingInstance::CompleteInputCallback(void* ctxt, const char* text, uint64_t state)
{
	ScriptingInstance* instance = (ScriptingInstance*)ctxt;
	const char* completed = instance->CompleteInput(text, state).c_str();
	if (completed == nullptr)
	{
		LogWarn("ScriptingInstance::CompleteInput returned nullptr; replacing with empty string.");
		completed = "";
	}
	return BNAllocString(completed);
}


void ScriptingInstance::StopCallback(void* ctxt)
{
	ScriptingInstance* instance = (ScriptingInstance*)ctxt;
	instance->Stop();
}


void ScriptingInstance::DestroyInstance()
{
	ReleaseForRegistration();
}<commit_msg>Fix lifetimes in ScriptingInstance::CompleteInputCallback<commit_after>#include "binaryninjaapi.h"

using namespace BinaryNinja;
using namespace std;


ScriptingOutputListener::ScriptingOutputListener()
{
	m_callbacks.context = this;
	m_callbacks.output = OutputCallback;
	m_callbacks.error = ErrorCallback;
	m_callbacks.inputReadyStateChanged = InputReadyStateChangedCallback;
}


void ScriptingOutputListener::OutputCallback(void* ctxt, const char* text)
{
	ScriptingOutputListener* listener = (ScriptingOutputListener*)ctxt;
	listener->NotifyOutput(text);
}


void ScriptingOutputListener::ErrorCallback(void* ctxt, const char* text)
{
	ScriptingOutputListener* listener = (ScriptingOutputListener*)ctxt;
	listener->NotifyError(text);
}


void ScriptingOutputListener::InputReadyStateChangedCallback(void* ctxt, BNScriptingProviderInputReadyState state)
{
	ScriptingOutputListener* listener = (ScriptingOutputListener*)ctxt;
	listener->NotifyInputReadyStateChanged(state);
}


void ScriptingOutputListener::NotifyOutput(const string&) {}


void ScriptingOutputListener::NotifyError(const string&) {}


void ScriptingOutputListener::NotifyInputReadyStateChanged(BNScriptingProviderInputReadyState) {}


ScriptingInstance::ScriptingInstance(ScriptingProvider* provider)
{
	BNScriptingInstanceCallbacks cb;
	cb.context = this;
	cb.destroyInstance = DestroyInstanceCallback;
	cb.externalRefTaken = nullptr;
	cb.externalRefReleased = nullptr;
	cb.executeScriptInput = ExecuteScriptInputCallback;
	cb.cancelScriptInput = CancelScriptInputCallback;
	cb.setCurrentBinaryView = SetCurrentBinaryViewCallback;
	cb.setCurrentFunction = SetCurrentFunctionCallback;
	cb.setCurrentBasicBlock = SetCurrentBasicBlockCallback;
	cb.setCurrentAddress = SetCurrentAddressCallback;
	cb.setCurrentSelection = SetCurrentSelectionCallback;
	cb.completeInput = CompleteInputCallback;
	cb.stop = StopCallback;
	AddRefForRegistration();
	m_object = BNInitScriptingInstance(provider->GetObject(), &cb);
}


ScriptingInstance::ScriptingInstance(BNScriptingInstance* instance)
{
	m_object = instance;
}


void ScriptingInstance::DestroyInstanceCallback(void* ctxt)
{
	ScriptingInstance* instance = (ScriptingInstance*)ctxt;
	instance->DestroyInstance();
}


BNScriptingProviderExecuteResult ScriptingInstance::ExecuteScriptInputCallback(void* ctxt, const char* input)
{
	ScriptingInstance* instance = (ScriptingInstance*)ctxt;
	return instance->ExecuteScriptInput(input);
}


void ScriptingInstance::CancelScriptInputCallback(void* ctxt)
{
	ScriptingInstance* instance = (ScriptingInstance*)ctxt;
	instance->CancelScriptInput();
}


void ScriptingInstance::SetCurrentBinaryViewCallback(void* ctxt, BNBinaryView* view)
{
	ScriptingInstance* instance = (ScriptingInstance*)ctxt;
	instance->SetCurrentBinaryView(view ? new BinaryView(BNNewViewReference(view)) : nullptr);
}


void ScriptingInstance::SetCurrentFunctionCallback(void* ctxt, BNFunction* func)
{
	ScriptingInstance* instance = (ScriptingInstance*)ctxt;
	instance->SetCurrentFunction(func ? new Function(BNNewFunctionReference(func)) : nullptr);
}


void ScriptingInstance::SetCurrentBasicBlockCallback(void* ctxt, BNBasicBlock* block)
{
	ScriptingInstance* instance = (ScriptingInstance*)ctxt;
	instance->SetCurrentBasicBlock(block ? new BasicBlock(BNNewBasicBlockReference(block)) : nullptr);
}


void ScriptingInstance::SetCurrentAddressCallback(void* ctxt, uint64_t addr)
{
	ScriptingInstance* instance = (ScriptingInstance*)ctxt;
	instance->SetCurrentAddress(addr);
}


void ScriptingInstance::SetCurrentSelectionCallback(void* ctxt, uint64_t begin, uint64_t end)
{
	ScriptingInstance* instance = (ScriptingInstance*)ctxt;
	instance->SetCurrentSelection(begin, end);
}


char* ScriptingInstance::CompleteInputCallback(void* ctxt, const char* text, uint64_t state)
{
	ScriptingInstance* instance = (ScriptingInstance*)ctxt;
	std::string completed = instance->CompleteInput(text, state);
	if (completed.c_str() == nullptr)
	{
		LogWarn("ScriptingInstance::CompleteInput returned nullptr; replacing with empty string.");
		completed = "";
	}
	return BNAllocString(completed.c_str());
}


void ScriptingInstance::StopCallback(void* ctxt)
{
	ScriptingInstance* instance = (ScriptingInstance*)ctxt;
	instance->Stop();
}


void ScriptingInstance::DestroyInstance()
{
	ReleaseForRegistration();
}
{'fraction_non_alphanumeric': 0.09981386691484412, 'fraction_numerical': 0.0018613308515588647, 'mean_word_length': 9.09037558685446, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1511052', 'n_tokens_mistral': 2481, 'n_tokens_neox': 2446, 'n_words': 428}