-
Notifications
You must be signed in to change notification settings - Fork 44
/
CEntryPoints.cpp
534 lines (491 loc) · 19.2 KB
/
CEntryPoints.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/**
Copyright (c) 2014-2015 National Instruments Corp.
This software is subject to the terms described in the LICENSE.TXT file
SDG
*/
/*! \file
\brief C entry points for core Vireo functions. Used when the runtime is built and loaded as a library
*/
#include "ExecutionContext.h"
#include "VirtualInstrument.h"
#include "TypeDefiner.h"
#include "TDCodecLVFlat.h"
#include "TDCodecVia.h"
#include "CEntryPoints.h"
#if defined (VIREO_C_ENTRY_POINTS)
namespace Vireo {
//------------------------------------------------------------
VIREO_EXPORT Int32 Vireo_Version()
{
// TODO(paul) need to tie into semantic version numbers
return 0x00020003;
}
//------------------------------------------------------------
//! Create a new shell with a designated parent, or null for a new root.
VIREO_EXPORT void* EggShell_Create(TypeManagerRef parent)
{
return TypeManager::New(parent);
}
//------------------------------------------------------------
VIREO_EXPORT NIError EggShell_REPL(TypeManagerRef tm, const Utf8Char* commands, Int32 length)
{
if (length == -1) {
length = (Int32)strlen((const char*)commands);
}
SubString input(commands, commands + length);
NIError err = TDViaParser::StaticRepl(tm, &input);
return err;
}
//------------------------------------------------------------
//! Run the vireo execution system for a few slices.
VIREO_EXPORT Int32 EggShell_ExecuteSlices(TypeManagerRef tm, Int32 numSlices)
{
TypeManagerScope scope(tm);
return tm->TheExecutionContext()->ExecuteSlices(numSlices, 20);
}
//------------------------------------------------------------
VIREO_EXPORT TypeRef EggShell_GetTypeList(TypeManagerRef tm)
{
return tm->TypeList();
}
//------------------------------------------------------------
//! Delete a shell and all the types it owns.
VIREO_EXPORT void EggShell_Delete(TypeManagerRef tm)
{
if (tm != null)
tm->Delete();
}
//------------------------------------------------------------
VIREO_EXPORT Int32 EggShell_PeekMemory(TypeManagerRef tm,
const char* viName, const char* eltName, Int32 bufferSize, char* buffer)
{
memset(buffer, 0, bufferSize);
void *pData = null;
SubString objectName(viName);
SubString path(eltName);
TypeRef actualType = tm->GetObjectElementAddressFromPath(&objectName, &path, &pData, true);
if (actualType == null)
return -1;
TypeManagerScope scope(tm);
STACK_VAR(String, flatDataString);
// Write flattened data to the string
FlattenData(actualType, pData, flatDataString.Value, true);
// Copy data to buffer
Int32 flatDataSize = flatDataString.Value->Length();
memcpy(buffer, flatDataString.Value->Begin(), Min(bufferSize, flatDataSize));
return flatDataSize;
}
//------------------------------------------------------------
VIREO_EXPORT Int32 EggShell_PokeMemory(TypeManagerRef tm,
const char* viName, const char* eltName, Int32 bufferSize, char* buffer)
{
void *pData = null;
SubString objectName(viName);
SubString path(eltName);
TypeRef actualType = tm->GetObjectElementAddressFromPath(&objectName, &path, &pData, true);
if (actualType == null)
return -1;
TypeManagerScope scope(tm);
SubBinaryBuffer subBuffer(reinterpret_cast<UInt8*>(buffer), reinterpret_cast<UInt8*>(buffer)+bufferSize);
// Write unflattened data to the element
if (UnflattenData(&subBuffer, true, 0, null, actualType, pData) == -1) {
return -1;
} else {
return bufferSize;
}
}
//------------------------------------------------------------
//! Write a numeric value to a symbol. Value will be coerced as needed.
VIREO_EXPORT void EggShell_WriteDouble(TypeManagerRef tm, const char* viName, const char* eltName, Double d)
{
void *pData = null;
SubString objectName(viName);
SubString path(eltName);
TypeRef actualType = tm->GetObjectElementAddressFromPath(&objectName, &path, &pData, true);
if (actualType == null)
return;
WriteDoubleToMemory(actualType, pData, d);
}
//------------------------------------------------------------
//! Read a numeric value from a symbol. Value will be coerced as needed.
VIREO_EXPORT Double EggShell_ReadDouble(TypeManagerRef tm, const char* viName, const char* eltName)
{
void *pData = null;
SubString objectName(viName);
SubString path(eltName);
TypeRef actualType = tm->GetObjectElementAddressFromPath(&objectName, &path, &pData, true);
if (actualType == null)
return -1;
return ReadDoubleFromMemory(actualType, pData);
}
//------------------------------------------------------------
// Write a string value to a symbol. Value will be parsed according to format designated.
VIREO_EXPORT void EggShell_WriteValueString(TypeManagerRef tm,
const char* viName, const char* eltName, const char* format, const char* value)
{
TypeManagerScope scope(tm);
void *pData = null;
SubString objectName(viName);
SubString path(eltName);
SubString valueString(value);
TypeRef actualType = tm->GetObjectElementAddressFromPath(&objectName, &path, &pData, true);
if (actualType == null)
return;
EventLog log(EventLog::DevNull);
SubString formatss(format);
TDViaParser parser(tm, &valueString, &log, 1, &formatss, true, true, true);
parser.ParseData(actualType, pData);
}
//------------------------------------------------------------
//! Read a symbol's value as a string. Value will be formatted according to the format designated.
VIREO_EXPORT const char* EggShell_ReadValueString(TypeManagerRef tm,
const char* viName, const char* eltName, const char* format)
{
TypeManagerScope scope(tm);
void *pData = null;
SubString objectName(viName);
SubString path(eltName);
TypeRef actualType = tm->GetObjectElementAddressFromPath(&objectName, &path, &pData, true);
if (actualType == null)
return null;
static StringRef returnBuffer = null;
if (returnBuffer == null) {
// Allocate a string the first time it is used.
// After that it will be resized as needed.
STACK_VAR(String, tempReturn);
returnBuffer = tempReturn.DetachValue();
} else {
returnBuffer->Resize1D(0);
}
if (returnBuffer) {
SubString formatss(format);
TDViaFormatter formatter(returnBuffer, true, 0, &formatss, true, true);
formatter.FormatData(actualType, pData);
// Add an explicit null terminator so it looks like a C string.
returnBuffer->Append((Utf8Char)'\0');
return (const char*) returnBuffer->Begin();
}
return "";
}
//------------------------------------------------------------
//! Get information about an Array such as the type of its subtype, the array rank,
//! and the memory location of the first element (or null if there are zero elements)
VIREO_EXPORT EggShellResult EggShell_GetArrayMetadata(TypeManagerRef tm,
const char* viName, const char* eltName, char** arrayTypeName, Int32* arrayRank, unsigned char** arrayBegin)
{
TypeManagerScope scope(tm);
void *pData = null;
SubString objectName(viName);
SubString path(eltName);
TypeRef pathType = tm->GetObjectElementAddressFromPath(&objectName, &path, &pData, true);
if (pathType == null)
return kEggShellResult_ObjectNotFoundAtPath;
if (!pathType->IsArray() || pathType->Rank() <= 0)
return kEggShellResult_UnexpectedObjectType;
if (arrayTypeName == null || arrayRank == null || arrayBegin == null)
return kEggShellResult_InvalidResultPointer;
static StringRef arrayTypeNameBuffer = null;
if (arrayTypeNameBuffer == null) {
// Allocate a string the first time it is used.
// After that it will be resized as needed.
STACK_VAR(String, tempReturn);
arrayTypeNameBuffer = tempReturn.DetachValue();
} else {
arrayTypeNameBuffer->Resize1D(0);
}
if (arrayTypeNameBuffer == null) {
return kEggShellResult_UnableToCreateReturnBuffer;
}
SubString arrayTypeNameSubString = pathType->GetSubElement(0)->Name();
arrayTypeNameBuffer->Append(arrayTypeNameSubString.Length(), (Utf8Char*)arrayTypeNameSubString.Begin());
arrayTypeNameBuffer->Append((Utf8Char)'\0');
*arrayTypeName = (char*) arrayTypeNameBuffer->Begin();
*arrayRank = pathType->Rank();
TypedArrayCoreRef actualArray = *(TypedArrayCoreRef*)pData;
if (actualArray->GetLength(0) <= 0) {
*arrayBegin = null;
} else {
*arrayBegin = actualArray->BeginAt(0);
}
return kEggShellResult_Success;
}
//------------------------------------------------------------
//! Get the Length of a dimension in an Array Symbol. Returns -1 if the Symbol is not found or not
//! an Array or dimension requested is out of the bounds of the rank.
VIREO_EXPORT Int32 EggShell_GetArrayDimLength(TypeManagerRef tm, const char* viName, const char* eltName, Int32 dim)
{
TypeManagerScope scope(tm);
void *pData = null;
SubString objectName(viName);
SubString path(eltName);
TypeRef actualType = tm->GetObjectElementAddressFromPath(&objectName, &path, &pData, true);
if (actualType == null || !actualType->IsArray() || dim >= actualType->Rank() || dim < 0)
return -1;
TypedArrayCoreRef actualArray = *(TypedArrayCoreRef*)pData;
return actualArray->GetLength(dim);
}
//------------------------------------------------------------
//! Resizes a variable size Array symbol to have new dimension lengths specified by newLengths, it also initializes cells for non-flat data.
//! Returns -1 if the symbols is not found, -2 if was not possible to resize the array and 0 if resizing was successful.
VIREO_EXPORT Int32 EggShell_ResizeArray(TypeManagerRef tm, const char* viName, const char* eltName, Int32 rank, Int32* newLengths)
{
TypeManagerScope scope(tm);
SubString objectName(viName);
SubString path(eltName);
void *pData = null;
TypeRef actualType = tm->GetObjectElementAddressFromPath(&objectName, &path, &pData, true);
if (actualType == null || !actualType->IsArray()) {
return kLVError_ArgError;
}
TypedArrayCoreRef actualArray = *(TypedArrayCoreRef*)pData;
if (!actualArray->ResizeDimensions(rank, newLengths, true, false)) {
return kLVError_MemFull;
}
return 0;
}
//------------------------------------------------------------
VIREO_EXPORT void* Data_GetStringBegin(StringRef stringObject)
{
VIREO_ASSERT(String::ValidateHandle(stringObject));
return stringObject->Begin();
}
//------------------------------------------------------------
VIREO_EXPORT Int32 Data_GetStringLength(StringRef stringObject)
{
VIREO_ASSERT(String::ValidateHandle(stringObject));
return stringObject->Length();
}
//------------------------------------------------------------
VIREO_EXPORT void Data_WriteString(TypeManagerRef tm, StringRef stringObject, const unsigned char* buffer, Int32 length)
{
VIREO_ASSERT(String::ValidateHandle(stringObject));
// Scope needs to be setup for allocations
TypeManagerScope scope(tm);
stringObject->CopyFrom(length, buffer);
}
//------------------------------------------------------------
VIREO_EXPORT Int32 Data_ReadBoolean(Boolean* booleanPointer)
{
return *booleanPointer;
}
//------------------------------------------------------------
VIREO_EXPORT void Data_WriteBoolean(Boolean* destination, Int32 value)
{
*destination = value;
}
//------------------------------------------------------------
VIREO_EXPORT void Data_WriteInt32(Int32* destination, Int32 value)
{
*destination = value;
}
//------------------------------------------------------------
VIREO_EXPORT void Data_WriteUInt32(UInt32* destination, UInt32 value)
{
*destination = value;
}
//------------------------------------------------------------
VIREO_EXPORT TypeRef TypeManager_Define(TypeManagerRef typeManager, const char* typeName, const char* typeString)
{
// TypeManagerScope scope(typeManager);
return TypeDefiner::Define(typeManager, typeName, typeString);
}
//------------------------------------------------------------
VIREO_EXPORT TypeRef TypeManager_FindType(TypeManagerRef typeManager, const char* typeName)
{
SubString temp(typeName);
return typeManager->FindType(&temp);
}
//------------------------------------------------------------
VIREO_EXPORT Int32 TypeRef_TopAQSize(TypeRef typeRef)
{
return typeRef->TopAQSize();
}
//------------------------------------------------------------
VIREO_EXPORT Boolean TypeRef_IsFlat(TypeRef typeRef)
{
return typeRef->IsFlat();
}
//------------------------------------------------------------
VIREO_EXPORT Boolean TypeRef_IsValid(TypeRef typeRef)
{
return typeRef->IsValid();
}
//------------------------------------------------------------
VIREO_EXPORT Boolean TypeRef_HasCustomDefault(TypeRef typeRef)
{
return typeRef->HasCustomDefault();
}
//------------------------------------------------------------
VIREO_EXPORT EncodingEnum TypeRef_BitEncoding(TypeRef typeRef)
{
return typeRef->BitEncoding();
}
//------------------------------------------------------------
VIREO_EXPORT Int32 TypeRef_Alignment(TypeRef typeRef)
{
return typeRef->AQAlignment();
}
//------------------------------------------------------------
VIREO_EXPORT void TypeRef_Name(TypeRef typeRef, Int32* bufferSize, char* buffer)
{
SubString name = typeRef->Name();
*bufferSize = name.CopyToBoundedBuffer(*bufferSize, reinterpret_cast<Utf8Char*>(buffer));
}
//------------------------------------------------------------
VIREO_EXPORT Int32 TypeRef_ElementOffset(TypeRef typeRef)
{
return typeRef->ElementOffset();
}
//------------------------------------------------------------
VIREO_EXPORT Int32 TypeRef_Rank(TypeRef typeRef)
{
return typeRef->Rank();
}
//------------------------------------------------------------
VIREO_EXPORT PointerTypeEnum TypeRef_PointerType(TypeRef typeRef)
{
return typeRef->PointerType();
}
//------------------------------------------------------------
VIREO_EXPORT TypeRef TypeRef_Next(TypeRef typeRef)
{
return typeRef->Next();
}
//------------------------------------------------------------
VIREO_EXPORT UsageTypeEnum TypeRef_ElementUsageType(TypeRef typeRef)
{
return typeRef->ElementUsageType();
}
//------------------------------------------------------------
VIREO_EXPORT Int32 TypeRef_SubElementCount(TypeRef typeRef)
{
return typeRef->SubElementCount();
}
//------------------------------------------------------------
VIREO_EXPORT TypeRef TypeRef_GetSubElementByIndex(TypeRef typeRef, Int32 index)
{
return typeRef->GetSubElement(index);
}
//------------------------------------------------------------
//------------------------------------------------------------
VIREO_EXPORT Int32 Data_RawBlockSize(TypedBlock* object)
{
VIREO_ASSERT(TypedBlock::ValidateHandle(object));
return object->AQBlockLength(object->Length());
}
//------------------------------------------------------------
VIREO_EXPORT Int32 Data_Length(TypedBlock* object)
{
VIREO_ASSERT(TypedBlock::ValidateHandle(object));
return object->Length();
}
//------------------------------------------------------------
VIREO_EXPORT TypeRef Data_Type(TypedBlock* object)
{
VIREO_ASSERT(TypedBlock::ValidateHandle(object));
return object->Type();
}
//------------------------------------------------------------
VIREO_EXPORT Int32 Data_GetLength(TypedBlock* object, Int32 dimension)
{
VIREO_ASSERT(TypedBlock::ValidateHandle(object));
return object->GetLength(dimension);
}
//------------------------------------------------------------
VIREO_EXPORT void Data_Resize1D(TypedBlock* object, Int32 size)
{
VIREO_ASSERT(TypedBlock::ValidateHandle(object));
object->Resize1D(size);
}
//------------------------------------------------------------
VIREO_EXPORT void Data_ResizeDimensions(TypedBlock* object, Int32 rank, IntIndex* sizes)
{
VIREO_ASSERT(TypedBlock::ValidateHandle(object));
object->ResizeDimensions(rank, sizes, false);
}
//------------------------------------------------------------
VIREO_EXPORT void* Data_RawPointerFromOffset(TypedBlock* object, Int32 offset)
{
VIREO_ASSERT(TypedBlock::ValidateHandle(object));
return object->RawBegin() + offset;
}
//------------------------------------------------------------
VIREO_EXPORT void Data_Read1Byte(TypedBlock* object, Int32 offset, Int8* value)
{
VIREO_ASSERT(TypedBlock::ValidateHandle(object));
*value = *object->BeginAtAQ<Int8*>(offset);
}
//------------------------------------------------------------
VIREO_EXPORT void Data_Write1Byte(TypedBlock* object, Int32 offset, Int8 value)
{
VIREO_ASSERT(TypedBlock::ValidateHandle(object));
*object->BeginAtAQ<Int8*>(offset) = value;
}
//------------------------------------------------------------
VIREO_EXPORT void Data_Read2Bytes(TypedBlock* object, Int32 offset, Int16* value)
{
VIREO_ASSERT(TypedBlock::ValidateHandle(object));
*value = *object->BeginAtAQ<Int16*>(offset);
}
//------------------------------------------------------------
VIREO_EXPORT void Data_Write2Bytes(TypedBlock* object, Int32 offset, Int16 value)
{
VIREO_ASSERT(TypedBlock::ValidateHandle(object));
*object->BeginAtAQ<Int16*>(offset) = value;
}
//------------------------------------------------------------
VIREO_EXPORT void Data_Read4Bytes(TypedBlock* object, Int32 offset, Int32* value)
{
VIREO_ASSERT(TypedBlock::ValidateHandle(object));
*value = *object->BeginAtAQ<Int32*>(offset);
}
//------------------------------------------------------------
VIREO_EXPORT void Data_Write4Bytes(TypedBlock* object, Int32 offset, Int32 value)
{
VIREO_ASSERT(TypedBlock::ValidateHandle(object));
*object->BeginAtAQ<Int32*>(offset) = value;
}
//------------------------------------------------------------
VIREO_EXPORT void Data_Read8Bytes(TypedBlock* object, Int32 offset, Int64* value)
{
VIREO_ASSERT(TypedBlock::ValidateHandle(object));
*value = *object->BeginAtAQ<Int64*>(offset);
}
//------------------------------------------------------------
VIREO_EXPORT void Data_Write8Bytes(TypedBlock* object, Int32 offset, Int64 value)
{
VIREO_ASSERT(TypedBlock::ValidateHandle(object));
*object->BeginAtAQ<Int64*>(offset) = value;
}
//------------------------------------------------------------
VIREO_EXPORT void Data_ReadPointer(TypedBlock* object, Int32 offset, void** value)
{
VIREO_ASSERT(TypedBlock::ValidateHandle(object));
*value = *object->BeginAtAQ<void**>(offset);
}
//------------------------------------------------------------
VIREO_EXPORT void Data_WritePointer(TypedBlock* object, Int32 offset, void* value)
{
VIREO_ASSERT(TypedBlock::ValidateHandle(object));
*object->BeginAtAQ<void**>(offset) = value;
}
//------------------------------------------------------------
VIREO_EXPORT void Data_ReadBytes(TypedBlock* object, Int32 offset, Int32 count, Int32* buffer)
{
VIREO_ASSERT(TypedBlock::ValidateHandle(object));
memcpy(buffer, object->BeginAtAQ<void*>(offset), count);
}
//------------------------------------------------------------
VIREO_EXPORT void Data_WriteBytes(TypedBlock* object, Int32 offset, Int32 count, Int32* buffer)
{
VIREO_ASSERT(TypedBlock::ValidateHandle(object));
memcpy(object->BeginAtAQ<void*>(offset), buffer, count);
}
//------------------------------------------------------------
VIREO_EXPORT void Occurrence_Set(OccurrenceRef occurrence)
{
OccurrenceCore *pOcc = occurrence->ObjBegin();
pOcc->SetOccurrence();
}
} // namespace Vireo
#endif // VIREO_C_ENTRY_POINTS