<commit_before>//
// ServiceRegistryWrapper.cpp
//
// $Id: //poco/1.4/OSP/JS/src/ServiceRegistryWrapper.cpp#4 $
//
// Copyright (c) 2013-2014, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: Apache-2.0
//


#include "Poco/OSP/JS/ServiceRegistryWrapper.h"
#include "Poco/OSP/JS/ServiceRefWrapper.h"


namespace Poco {
namespace OSP {
namespace JS {


ServiceRegistryWrapper::ServiceRegistryWrapper()
{
}


ServiceRegistryWrapper::~ServiceRegistryWrapper()
{
}


v8::Handle<v8::ObjectTemplate> ServiceRegistryWrapper::objectTemplate(v8::Isolate* pIsolate)
{
	v8::EscapableHandleScope handleScope(pIsolate);
	v8::Local<v8::ObjectTemplate> serviceRegistryTemplate = v8::ObjectTemplate::New(pIsolate);
	serviceRegistryTemplate->SetInternalFieldCount(1);
	serviceRegistryTemplate->Set(v8::String::NewFromUtf8(pIsolate, "find"), v8::FunctionTemplate::New(pIsolate, find));
	serviceRegistryTemplate->Set(v8::String::NewFromUtf8(pIsolate, "findByName"), v8::FunctionTemplate::New(pIsolate, findByName));
	return handleScope.Escape(serviceRegistryTemplate);
}


void ServiceRegistryWrapper::findByName(const v8::FunctionCallbackInfo<v8::Value>& args)
{
	Poco::OSP::ServiceRegistry* pServiceRegistry = Poco::JS::Core::Wrapper::unwrapNative<Poco::OSP::ServiceRegistry>(args);

	try
	{
		if (args.Length() == 1)
		{
			std::string serviceName = toString(args[0]);
			Poco::OSP::ServiceRef::Ptr pServiceRef = pServiceRegistry->findByName(serviceName);
			if (pServiceRef)
			{
				ServiceRefWrapper wrapper;
				v8::Persistent<v8::Object> serviceRefObject(args.GetIsolate(), wrapper.wrapNativePersistent(args.GetIsolate(), pServiceRef));
				args.GetReturnValue().Set(serviceRefObject);
				return;
			}
			args.GetReturnValue().Set(v8::Null(args.GetIsolate()));
		}
		else
		{
			returnException(args, std::string("bad arguments - service name required"));
		}
	}
	catch (Poco::Exception& exc)
	{
		returnException(args, exc);
	}
}


void ServiceRegistryWrapper::find(const v8::FunctionCallbackInfo<v8::Value>& args)
{
	Poco::OSP::ServiceRegistry* pServiceRegistry = Poco::JS::Core::Wrapper::unwrapNative<Poco::OSP::ServiceRegistry>(args);

	try<commit_msg>fix a JS memory leak in findByName()<commit_after>//
// ServiceRegistryWrapper.cpp
//
// $Id: //poco/1.4/OSP/JS/src/ServiceRegistryWrapper.cpp#4 $
//
// Copyright (c) 2013-2014, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: Apache-2.0
//


#include "Poco/OSP/JS/ServiceRegistryWrapper.h"
#include "Poco/OSP/JS/ServiceRefWrapper.h"


namespace Poco {
namespace OSP {
namespace JS {


ServiceRegistryWrapper::ServiceRegistryWrapper()
{
}


ServiceRegistryWrapper::~ServiceRegistryWrapper()
{
}


v8::Handle<v8::ObjectTemplate> ServiceRegistryWrapper::objectTemplate(v8::Isolate* pIsolate)
{
	v8::EscapableHandleScope handleScope(pIsolate);
	v8::Local<v8::ObjectTemplate> serviceRegistryTemplate = v8::ObjectTemplate::New(pIsolate);
	serviceRegistryTemplate->SetInternalFieldCount(1);
	serviceRegistryTemplate->Set(v8::String::NewFromUtf8(pIsolate, "find"), v8::FunctionTemplate::New(pIsolate, find));
	serviceRegistryTemplate->Set(v8::String::NewFromUtf8(pIsolate, "findByName"), v8::FunctionTemplate::New(pIsolate, findByName));
	return handleScope.Escape(serviceRegistryTemplate);
}


void ServiceRegistryWrapper::findByName(const v8::FunctionCallbackInfo<v8::Value>& args)
{
	Poco::OSP::ServiceRegistry* pServiceRegistry = Poco::JS::Core::Wrapper::unwrapNative<Poco::OSP::ServiceRegistry>(args);

	try
	{
		if (args.Length() == 1)
		{
			std::string serviceName = toString(args[0]);
			Poco::OSP::ServiceRef::Ptr pServiceRef = pServiceRegistry->findByName(serviceName);
			if (pServiceRef)
			{
				ServiceRefWrapper wrapper;
				v8::Persistent<v8::Object>& serviceRefObject(wrapper.wrapNativePersistent(args.GetIsolate(), pServiceRef));
				args.GetReturnValue().Set(serviceRefObject);
				return;
			}
			args.GetReturnValue().Set(v8::Null(args.GetIsolate()));
		}
		else
		{
			returnException(args, std::string("bad arguments - service name required"));
		}
	}
	catch (Poco::Exception& exc)
	{
		returnException(args, exc);
	}
}


void ServiceRegistryWrapper::find(const v8::FunctionCallbackInfo<v8::Value>& args)
{
	Poco::OSP::ServiceRegistry* pServiceRegistry = Poco::JS::Core::Wrapper::unwrapNative<Poco::OSP::ServiceRegistry>(args);

	try
{'fraction_non_alphanumeric': 0.15358361774744028, 'fraction_numerical': 0.016382252559726963, 'mean_word_length': 6.920720720720721, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 25, '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': '6711494', 'n_tokens_mistral': 1533, 'n_tokens_neox': 1439, 'n_words': 194}