<commit_before>// Copyright (c) 2013, <NAME> and the Dart project authors.
// 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/VM.h"
#include "cidart/Script.h"
#include "cidart/Types.h"
#include "cidart/Debug.h"

#include "cinder/app/App.h"
#include "cinder/Utilities.h"
#include "cinder/CinderAssert.h"

#include <map>

#define PROFILE_LOAD_TIME 0

#if PROFILE_LOAD_TIME
	#include "cinder/Timer.h"
#endif

using namespace std;
using namespace ci;

namespace cidart {

// static
VM* VM::instance()
{
	static std::unique_ptr<VM>	sInstance;
	if( ! sInstance )
		sInstance.reset( new VM );

	return sInstance.get();
}

VM::VM()
{
	// TODO: pass in vm flags at construction.
	mVMFlags.push_back( "--checked" );
	mVMFlags.push_back( "--no-profile" ); // currently dart's profiler seems to be blocking the main thread when debugging in xcode - this disables it for now
//	mVMFlags.push_back( "--print-flags" );

	const char **vmCFlags = (const char **)malloc( mVMFlags.size() * sizeof( const char * ) );
	for( size_t i = 0; i < mVMFlags.size(); i++ )
		vmCFlags[i] = mVMFlags[i].c_str();

	// setting VM startup options
	bool success = Dart_SetVMFlags( mVMFlags.size(), vmCFlags );
	CI_VERIFY( success );
	free( vmCFlags );

	success = Dart_Initialize(	Script::createIsolateCallback,
								interruptIsolateCallback,
								unhandledExceptionCallback,
								shutdownIsolateCallback,
								openFileCallback,
								readFileCallback,
								writeFileCallback,
								closeFileCallback,
								NULL,
								NULL );
	CI_VERIFY( success );
}

void VM::loadCinderDartLib()
{
	string script = getCinderDartScript();
	Dart_Handle source = toDart( script );
	CIDART_CHECK( source );

	Dart_Handle cinderDartLib = Dart_LoadLibrary( toDart( "cinder.dart" ), source, 0, 0 );
	CIDART_CHECK( cinderDartLib );
	CIDART_CHECK( Dart_SetNativeResolver( cinderDartLib, Script::resolveNameHandler, NULL ) );

	// finalize any scripts loaded, needs to be done before the libs can be looked up and modified below
	CIDART_CHECK( Dart_FinalizeLoading( false ) );

	// swap in custom _printClosure to enable print() in dart
	Dart_Handle internalLib = Dart_LookupLibrary( toDart( "dart:_internal" ) );
	CIDART_CHECK( internalLib );
	Dart_Handle print = Dart_GetField( cinderDartLib, toDart( "_printClosure" ) );
	CIDART_CHECK( print );
	CIDART_CHECK( Dart_SetField( internalLib, toDart( "_printClosure" ), print ) );
}

// static
const char* VM::getVersionString()
{
	return Dart_VersionString();
}

string VM::getCinderDartScript()
{
	if( mCinderDartScriptDataSource )
		return loadString( mCinderDartScriptDataSource );

#if defined( CINDER_COCOA )
	if( mCinderDartScriptPath.empty() ) {
		// attempt to load it as a resource added to the project
		auto resource = app::loadResource( "cinder.dart" );
		mCinderDartScriptPath = resource->getFilePath();
	}
#endif

	return loadString( loadFile( mCinderDartScriptPath ) );
}

const DataSourceRef& VM::getSnapShot()
{
	if( ! mSnapshot ) {
		if( ! mSnapshotPath.empty() ) {
			mSnapshot = loadFile( mSnapshotPath );
		}
#if defined( CINDER_COCOA )
		else {
			mSnapshot = app::loadResource( "snapshot_gen.bin" );
		}
#endif
	}

	return mSnapshot;
}

//// ----------------------------------------------------------------------------------------------------
//// MARK: - Dart Callbacks
//// ----------------------------------------------------------------------------------------------------<commit_msg>throw exception if cinder.dart can't be resolved<commit_after>// Copyright (c) 2013, <NAME> and the Dart project authors.
// 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/VM.h"
#include "cidart/Script.h"
#include "cidart/Types.h"
#include "cidart/Debug.h"

#include "cinder/app/App.h"
#include "cinder/Utilities.h"
#include "cinder/CinderAssert.h"

#include <map>

#define PROFILE_LOAD_TIME 0

#if PROFILE_LOAD_TIME
	#include "cinder/Timer.h"
#endif

using namespace std;
using namespace ci;

namespace cidart {

// static
VM* VM::instance()
{
	static std::unique_ptr<VM>	sInstance;
	if( ! sInstance )
		sInstance.reset( new VM );

	return sInstance.get();
}

VM::VM()
{
	// TODO: pass in vm flags at construction.
	mVMFlags.push_back( "--checked" );
	mVMFlags.push_back( "--no-profile" ); // currently dart's profiler seems to be blocking the main thread when debugging in xcode - this disables it for now
//	mVMFlags.push_back( "--print-flags" );

	const char **vmCFlags = (const char **)malloc( mVMFlags.size() * sizeof( const char * ) );
	for( size_t i = 0; i < mVMFlags.size(); i++ )
		vmCFlags[i] = mVMFlags[i].c_str();

	// setting VM startup options
	bool success = Dart_SetVMFlags( mVMFlags.size(), vmCFlags );
	CI_VERIFY( success );
	free( vmCFlags );

	success = Dart_Initialize(	Script::createIsolateCallback,
								interruptIsolateCallback,
								unhandledExceptionCallback,
								shutdownIsolateCallback,
								openFileCallback,
								readFileCallback,
								writeFileCallback,
								closeFileCallback,
								NULL,
								NULL );
	CI_VERIFY( success );
}

void VM::loadCinderDartLib()
{
	string script = getCinderDartScript();
	Dart_Handle source = toDart( script );
	CIDART_CHECK( source );

	Dart_Handle cinderDartLib = Dart_LoadLibrary( toDart( "cinder.dart" ), source, 0, 0 );
	CIDART_CHECK( cinderDartLib );
	CIDART_CHECK( Dart_SetNativeResolver( cinderDartLib, Script::resolveNameHandler, NULL ) );

	// finalize any scripts loaded, needs to be done before the libs can be looked up and modified below
	CIDART_CHECK( Dart_FinalizeLoading( false ) );

	// swap in custom _printClosure to enable print() in dart
	Dart_Handle internalLib = Dart_LookupLibrary( toDart( "dart:_internal" ) );
	CIDART_CHECK( internalLib );
	Dart_Handle print = Dart_GetField( cinderDartLib, toDart( "_printClosure" ) );
	CIDART_CHECK( print );
	CIDART_CHECK( Dart_SetField( internalLib, toDart( "_printClosure" ), print ) );
}

// static
const char* VM::getVersionString()
{
	return Dart_VersionString();
}

string VM::getCinderDartScript()
{
	if( mCinderDartScriptDataSource )
		return loadString( mCinderDartScriptDataSource );

#if defined( CINDER_COCOA )
	if( mCinderDartScriptPath.empty() ) {
		// attempt to load it as a resource added to the project
		auto resource = app::loadResource( "cinder.dart" );
		mCinderDartScriptPath = resource->getFilePath();
	}
#endif

	if( ! mCinderDartScriptPath.empty() )
		return loadString( loadFile( mCinderDartScriptPath ) );

	throw DartException( "no provided cinder.dart script file" );
}

const DataSourceRef& VM::getSnapShot()
{
	if( ! mSnapshot ) {
		if( ! mSnapshotPath.empty() ) {
			mSnapshot = loadFile( mSnapshotPath );
		}
#if defined( CINDER_COCOA )
		else {
			mSnapshot = app::loadResource( "snapshot_gen.bin" );
		}
#endif
	}

	return mSnapshot;
}

//// ----------------------------------------------------------------------------------------------------
//// MARK: - Dart Callbacks
//// ----------------------------------------------------------------------------------------------------
{'fraction_non_alphanumeric': 0.17087108013937283, 'fraction_numerical': 0.002229965156794425, 'mean_word_length': 3.2063305978898007, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '1631075', 'n_tokens_mistral': 2538, 'n_tokens_neox': 2321, 'n_words': 609}