<commit_before>// Copyright (c) 2014, <NAME>
// Use of this source code (and the Dart VM) is governed by a
// BSD-style license that can be found in the LICENSE.txt file.

#include "cidart/Script.h"
#include "cidart/VM.h"
#include "cidart/Types.h"
#include "cidart/Debug.h"

#include "cinder/Utilities.h"

#define PROFILE_LOAD_TIME 0

using namespace ci;
using namespace std;

namespace cidart {

Script::Options& Script::Options::native( const string &key, Dart_NativeFunction nativeFn )
{
	mNativeCallbackMap[key] = nativeFn;
	return *this;
}

Script::Options& Script::Options::native( const string &key, const FunctionCallback &nativeFn )
{
	mFunctionCallbackMap[key] = nativeFn;
	return *this;
}

Script::Script( const fs::path &sourcePath, const Options &options )
	: mIsolate( nullptr ), mMainScriptPath( sourcePath ),
		mNativeCallbackMap( options.getNativeCallbackMap() ), mFunctionCallbackMap( options.getFunctionCallbackMap() ), mReceiveMapCallback( options.getReceiveMapCallback() )
{
	init();
}

Script::Script( const DataSourceRef &source, const Options &options )
	: mIsolate( nullptr ), mMainScriptPath( source->getFilePath() ),
		mNativeCallbackMap( options.getNativeCallbackMap() ), mFunctionCallbackMap( options.getFunctionCallbackMap() ), mReceiveMapCallback( options.getReceiveMapCallback() )
{
	init();
}

void Script::init()
{
	if( mMainScriptPath.empty() || ! fs::exists( mMainScriptPath ) || fs::is_directory( mMainScriptPath ) )
		throw DartException( "invalid script path: " + mMainScriptPath.string() );

	mNativeCallbackMap["cidart::printNative"] = printNativeHandler;
	mNativeCallbackMap["cidart::toCinder"] = toCinderHandler;
	mNativeCallbackMap["cidart::callNative0"] = callNativeFunctionHandler;
	mNativeCallbackMap["cidart::callNative1"] = callNativeFunctionHandler;
	mNativeCallbackMap["cidart::callNative2"] = callNativeFunctionHandler;
	mNativeCallbackMap["cidart::callNative3"] = callNativeFunctionHandler;
	mNativeCallbackMap["cidart::callNative4"] = callNativeFunctionHandler;
	mNativeCallbackMap["cidart::callNative5"] = callNativeFunctionHandler;
	mNativeCallbackMap["cidart::getWindowSize"] = getWindowSize;
	mNativeCallbackMap["cidart::getWindowWidth"] = getWindowWidth;
	mNativeCallbackMap["cidart::getWindowHeight"] = getWindowHeight;

	const char *sourcePath = mMainScriptPath.string().c_str();

	char *error;
	mIsolate = Script::createIsolateCallback( sourcePath, "main", NULL, this, &error );
	if( ! mIsolate )
		throw DartException( "could not create isolate, error: " + string( error ) );

	DartScope enterScope;

	VM::instance()->loadCinderDartLib();

	Dart_Handle url = toDart( sourcePath );
	string sourceStr = loadSourceImpl( mMainScriptPath );

	Dart_Handle sourceHandle = toDart( sourceStr );
	CIDART_CHECK( sourceHandle );

	Dart_Handle scriptHandle = Dart_LoadScript( url, sourceHandle, 0, 0 );
	if( Dart_IsError( scriptHandle ) )
		throw DartException( Dart_GetError( scriptHandle ) );

	CIDART_CHECK( Dart_SetNativeResolver( Dart_RootLibrary(), resolveNameHandler, NULL ) );

	// finalize script and invoke main.
	CIDART_CHECK( Dart_FinalizeLoading( false ) );
	invoke( "main" );
}

Dart_Handle Script::invoke( const string &functionName, int argc, Dart_Handle *args )
{
	Dart_Handle library = Dart_RootLibrary();
	CI_ASSERT( ! Dart_IsNull( library ) );

	Dart_Handle nameHandle = toDart( functionName.c_str() );
	Dart_Handle result = Dart_Invoke( library, nameHandle, argc, args );

	if( Dart_IsError( result ) )
		throw DartException( Dart_GetError( result ) );

	return result;
}

string Script::loadSourceImpl( const fs::path &sourcePath )
{
	try {
		return loadString( loadFile( sourcePath ) );
	}
	catch( ci::StreamExc &exc ) {
		throw DartException( "failed to load source path (StreamExc caught): " + sourcePath.string() );
	}
}

// ----------------------------------------------------------------------------------------------------
// MARK: - Dart API Callbacks
// ----------------------------------------------------------------------------------------------------

// static
Dart_Isolate Script::createIsolateCallback( const char *scriptUri, const char *main, const char *packageRoot, void *callbackData, char **error )
{
	VM *vm = VM::instance();

	uint8_t *snapshotData = nullptr;
	auto snapshot = vm->getSnapShot();
	if( snapshot )
		snapshotData = (uint8_t *) snapshot->getBuffer().getData();

	Dart_Isolate isolate = Dart_CreateIsolate( scriptUri, main, snapshotData, callbackData, error );
	if ( ! isolate ) {
		CI_LOG_E( "Couldn't create isolate: " << *error );
		return nullptr;<commit_msg>update for Buffer / BufferRef changes<commit_after>// Copyright (c) 2014, <NAME>
// Use of this source code (and the Dart VM) is governed by a
// BSD-style license that can be found in the LICENSE.txt file.

#include "cidart/Script.h"
#include "cidart/VM.h"
#include "cidart/Types.h"
#include "cidart/Debug.h"

#include "cinder/Utilities.h"

#define PROFILE_LOAD_TIME 0

using namespace ci;
using namespace std;

namespace cidart {

Script::Options& Script::Options::native( const string &key, Dart_NativeFunction nativeFn )
{
	mNativeCallbackMap[key] = nativeFn;
	return *this;
}

Script::Options& Script::Options::native( const string &key, const FunctionCallback &nativeFn )
{
	mFunctionCallbackMap[key] = nativeFn;
	return *this;
}

Script::Script( const fs::path &sourcePath, const Options &options )
	: mIsolate( nullptr ), mMainScriptPath( sourcePath ),
		mNativeCallbackMap( options.getNativeCallbackMap() ), mFunctionCallbackMap( options.getFunctionCallbackMap() ), mReceiveMapCallback( options.getReceiveMapCallback() )
{
	init();
}

Script::Script( const DataSourceRef &source, const Options &options )
	: mIsolate( nullptr ), mMainScriptPath( source->getFilePath() ),
		mNativeCallbackMap( options.getNativeCallbackMap() ), mFunctionCallbackMap( options.getFunctionCallbackMap() ), mReceiveMapCallback( options.getReceiveMapCallback() )
{
	init();
}

void Script::init()
{
	if( mMainScriptPath.empty() || ! fs::exists( mMainScriptPath ) || fs::is_directory( mMainScriptPath ) )
		throw DartException( "invalid script path: " + mMainScriptPath.string() );

	mNativeCallbackMap["cidart::printNative"] = printNativeHandler;
	mNativeCallbackMap["cidart::toCinder"] = toCinderHandler;
	mNativeCallbackMap["cidart::callNative0"] = callNativeFunctionHandler;
	mNativeCallbackMap["cidart::callNative1"] = callNativeFunctionHandler;
	mNativeCallbackMap["cidart::callNative2"] = callNativeFunctionHandler;
	mNativeCallbackMap["cidart::callNative3"] = callNativeFunctionHandler;
	mNativeCallbackMap["cidart::callNative4"] = callNativeFunctionHandler;
	mNativeCallbackMap["cidart::callNative5"] = callNativeFunctionHandler;
	mNativeCallbackMap["cidart::getWindowSize"] = getWindowSize;
	mNativeCallbackMap["cidart::getWindowWidth"] = getWindowWidth;
	mNativeCallbackMap["cidart::getWindowHeight"] = getWindowHeight;

	const char *sourcePath = mMainScriptPath.string().c_str();

	char *error;
	mIsolate = Script::createIsolateCallback( sourcePath, "main", NULL, this, &error );
	if( ! mIsolate )
		throw DartException( "could not create isolate, error: " + string( error ) );

	DartScope enterScope;

	VM::instance()->loadCinderDartLib();

	Dart_Handle url = toDart( sourcePath );
	string sourceStr = loadSourceImpl( mMainScriptPath );

	Dart_Handle sourceHandle = toDart( sourceStr );
	CIDART_CHECK( sourceHandle );

	Dart_Handle scriptHandle = Dart_LoadScript( url, sourceHandle, 0, 0 );
	if( Dart_IsError( scriptHandle ) )
		throw DartException( Dart_GetError( scriptHandle ) );

	CIDART_CHECK( Dart_SetNativeResolver( Dart_RootLibrary(), resolveNameHandler, NULL ) );

	// finalize script and invoke main.
	CIDART_CHECK( Dart_FinalizeLoading( false ) );
	invoke( "main" );
}

Dart_Handle Script::invoke( const string &functionName, int argc, Dart_Handle *args )
{
	Dart_Handle library = Dart_RootLibrary();
	CI_ASSERT( ! Dart_IsNull( library ) );

	Dart_Handle nameHandle = toDart( functionName.c_str() );
	Dart_Handle result = Dart_Invoke( library, nameHandle, argc, args );

	if( Dart_IsError( result ) )
		throw DartException( Dart_GetError( result ) );

	return result;
}

string Script::loadSourceImpl( const fs::path &sourcePath )
{
	try {
		return loadString( loadFile( sourcePath ) );
	}
	catch( ci::StreamExc &exc ) {
		throw DartException( "failed to load source path (StreamExc caught): " + sourcePath.string() );
	}
}

// ----------------------------------------------------------------------------------------------------
// MARK: - Dart API Callbacks
// ----------------------------------------------------------------------------------------------------

// static
Dart_Isolate Script::createIsolateCallback( const char *scriptUri, const char *main, const char *packageRoot, void *callbackData, char **error )
{
	VM *vm = VM::instance();

	uint8_t *snapshotData = nullptr;
	auto snapshot = vm->getSnapShot();
	if( snapshot )
		snapshotData = (uint8_t *)snapshot->getBuffer()->getData();

	Dart_Isolate isolate = Dart_CreateIsolate( scriptUri, main, snapshotData, callbackData, error );
	if ( ! isolate ) {
		CI_LOG_E( "Couldn't create isolate: " << *error );
		return nullptr;
{'fraction_non_alphanumeric': 0.1625927542557835, 'fraction_numerical': 0.003273679615888258, 'mean_word_length': 4.678438661710037, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 14, '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': '3268285', 'n_tokens_mistral': 2835, 'n_tokens_neox': 2746, 'n_words': 649}